Android Bitmap to Byte Array Convert bmpToByteArray(final Bitmap bmp, final boolean needRecycle)

Here you can find the source of bmpToByteArray(final Bitmap bmp, final boolean needRecycle)

Description

bmp To Byte Array

License

Open Source License

Declaration

public static byte[] bmpToByteArray(final Bitmap bmp,
        final boolean needRecycle) 

Method Source Code

//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) {
                }
            }
        }
    }
}

Related

  1. Bitmap2Bytes(Bitmap bm)
  2. bitmap2Bytes(Bitmap bm)
  3. bitmapToByteArray(Bitmap bmp)
  4. bitmapToBytes(Bitmap bm)
  5. convertBitmapToByte(Bitmap bitmap)
  6. getBitmapData(Bitmap bmp)
  7. bitmapTobyte(Bitmap bmp)
  8. Bitmap2Bytes(Bitmap bm)