youssefhassan youssefhassan - 15 days ago 8
Android Question

Android Converting Ninepatch Resource To Bitmap

I have a 9 patch drawable as XML. When I try to use

BitmapFactory.decode
to get bitmap out of it, it returns
null
. Is there anyway to get a bitmap from this resource?

nine_patch.xml:

<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/empty_icon_video" >

</nine-patch>


and the code is:

BitmapFactory.decodeResource(mResources, resId);

Answer
Bitmap bmp = Bitmap.createBitmap(yourWidth, yourHeight, Bitmap.Config.ARGB_8888);
Drawable drawable = getResources().getDrawable(R.drawable.resId);
Canvas canvas = new Canvas(bmp);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);

Try this. Now the bitmap will be having the ninepatch drawn over it. You can supply a different width and height.