Here you can find the source of bmpToByteArray(final Bitmap bmp, final boolean needRecycle)
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle)
//License from project: Open Source License import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import org.ixming.android.utils.FrameworkLog; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; public class Main{ static final String TAG = "sharebind_bitmaputil"; /*from w w w . j a v a 2 s. c o m*/ public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) { if (null == bmp) { return null; } ByteArrayOutputStream output = null; try { output = new ByteArrayOutputStream(); bmp.compress(CompressFormat.PNG, 100, output); if (needRecycle) { bmp.recycle(); } byte[] result = output.toByteArray(); return result; } catch (Exception e) { FrameworkLog.e(TAG, "bmpToByteArray Exception: " + e.getMessage()); return null; } finally { if (null != output) { try { output.close(); } catch (Exception ex) { } } } } }