youssefhassan youssefhassan - 10 months ago 81
Android Question

Android Converting Ninepatch Resource To Bitmap

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

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


<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android=""
android:src="@drawable/empty_icon_video" >


and the code is:

BitmapFactory.decodeResource(mResources, resId);

Answer Source
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());

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