BitmapFactory.decodeFile out of memory with images 2400x2400

Posted by alasarr on Stack Overflow See other posts from Stack Overflow or by alasarr
Published on 2013-10-30T09:39:54Z Indexed on 2013/10/30 9:54 UTC
Read the original article Hit count: 142

Filed under:
|

I need to send an image from a file to a server. The server request the image in a resolution of 2400x2400.

What I'm trying to do is:

1) Get a Bitmap using BitmapFactory.decodeFile using the correct inSampleSize.

2) Compress the image in JPEG with a quality of 40%

3) Encode the image in base64

4) Sent to the server

I cannot achieve the first step, it throws an out of memory exception. I'm sure the inSampleSize is correct but I suppose even with inSampleSize the Bitmap is huge (around 30 MB in DDMS).

Any ideas how can do it? Can I do these steps without created a bitmap object? I mean doing it on filesystem instead of RAM memory.

This is the current code:

// The following function calculate the correct inSampleSize
Bitmap image = Util.decodeSampledBitmapFromFile(imagePath, width,height);   
// compressing the image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 40, baos);
// encode image
String encodedImage = Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT));

© Stack Overflow or respective owner

Related posts about android

Related posts about bitmap