Java tutorial
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; public class Main { /** * Bitmap -> byte * * @param bitmap * @return */ public static byte[] drawableToByte(Drawable drawable) { Bitmap bitmap = drawableToBitmap(drawable); return bitmapToByte(bitmap); } /** * Drawable -> bitmap * * @param drawable * @return */ public static Bitmap drawableToBitmap(Drawable drawable) { BitmapDrawable bd = (BitmapDrawable) drawable; Bitmap bm = bd.getBitmap(); return bm; } /** * Bitmap -> byte * * @param bitmap * @return */ public static byte[] bitmapToByte(Bitmap bitmap) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); bitmap.recycle(); bitmap = null; byte[] array = out.toByteArray(); return array; } catch (Exception e) { e.printStackTrace(); } return null; } }