0

I want to convert an image in my app to a Base64 encoded string. This image may be of any type like jpeg, png etc.

What I have done is, I converted the drawable to a Bitmap. Then I converted this Bitmap to ByteArrayOutputStream using compress metheod And I am converting this ByteArrayOutputStream to byte array. And then I am encoding it to Base64 using encodeToString().

I can display the image using the above method if the image is of PNG or JPEG.

ByteArrayOutputStream objByteOutput = new ByteArrayOutputStream();
 imgBitmap.compress(CompressFormat.JPEG, 0, objByteOutput);

But the problem is if the image is in any other types than PNG or JPEG, how can I display the image?

Or please suggest me some another method to get byte array from Bitmap.

Thank you...

1
  • 1
    If your issue is displaying the image in Android I think the only supported formats are jpg, png and gif, so it would be logical that other s formats don't work... Then I guess it's the conversion to bitmap that is messing things up. tkae a look inside the code of the compress method and try to duplicate it to accept any kind of file for you ? Commented Sep 29, 2011 at 8:09

1 Answer 1

1

I'd suggest using

http://developer.android.com/reference/android/graphics/Bitmap.html#copyPixelsToBuffer(java.nio.Buffer)

and specify a ByteBuffer, then you can use .array() on the ByteBuffer if it is implemented (it's an optional method) or .get(byte[]) to get it if .array() doesn't exist.

Update:

In order to determine the size of the buffer to create you should use Bitmap.getByteCount(). However this is only present on API 12 and up, so you would need to use Bitmap.getWidth()*Bitmap.getHeight()*4 - the reason for 4 is that the Bitmap uses a series of pixels (internal representation may be less but shouldn't ever be more), each being an ARGB value with 0-255 hence 4 bytes per pixel.

You can get the same with Bitmap.getHeight() * Bitmap.getRowBytes() - here's some code I used to verify this worked:

            BitmapDrawable bmd = (BitmapDrawable) getResources().getDrawable(R.drawable.icon);
            Bitmap bm = bmd.getBitmap();
            ByteBuffer byteBuff = ByteBuffer.allocate(bm.getWidth() * bm.getHeight() * 4);
            byteBuff.rewind();
            bm.copyPixelsToBuffer(byteBuff);
            byte[] tmp = new byte[bm.getWidth() * bm.getHeight() * 4];
            byteBuff.rewind();
            byteBuff.get(tmp);

It's not nice code, but it gets the byte array out.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks zeetoobiker for the help.. Itried with ByteBuffer, but I ma getting the exception 'Buffer not large enough for pixels'. I set the buffer capacity as imgBitmap.getHeight()*imgBitmap.getWidth(). Then also I ma getting the same exception. How can we know what would be the excat buffer size we should initialise?
You're calculating in ints not bytes you want to use Bitmap.getByteCount() as width*height is really a multiple number of bytes per pixel (depends on format specifically) to find out how big it really would be.
but getByteCount() is supported by API level 12 onwards only. I am using API level 8 only
Whoops, sorry I should have read that it was introduced late. Ok, the answer is then probably related to getRowBytes() but I'm not entirely sure how - I'd suggest just multiplying by 4 as that's the top value as far as I can tell (argb - one byte per) from the Colour class - "The components are stored as follows (alpha << 24) | (red << 16) | (green << 8) | blue." I have to admit when I've done it myself I just multiplied by 4 and reused the objects once I've extracted.
BitmapCompat.getAllocationByteCount(bitmap);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.