Java tutorial
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; public class Main { /** * convert Drawable to byte array * * @param d * @return */ public static byte[] drawableToByte(Drawable d) { return bitmapToByte(drawableToBitmap(d)); } /** * convert Bitmap to byte array * * @param b * @return */ public static byte[] bitmapToByte(Bitmap b) { if (b == null) { return null; } ByteArrayOutputStream o = new ByteArrayOutputStream(); b.compress(CompressFormat.PNG, 100, o); return o.toByteArray(); } public static Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Config.ARGB_8888 : Config.RGB_565); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, width, height); drawable.draw(canvas); return bitmap; } }