Search code examples
androidbitmapuriout-of-memoryandroid-gallery

Android - Outofmemory error by decodefile or decodeStream to convert the Uri selected to bitmap


I have a big problem with the function to choose a picture in the gallery's phone ! I chose the photo, I get the URI and when a read the stream in the "onActivityResult" for put the stream in a bitmap I have a "outoffmemoryerror".

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
       Uri uri = data.getData();
       InputStream is = getContentResolver().openInputStream(mSelectedUri);
       Bitmap bitmap = BitmapFactory.decodeStream(is);
}

I have tried other code but I have the same problem. Well, depends the picture !

I have find this : http://developer.android.com/training/displaying-bitmaps/index.html And http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

with this : "To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before decoding it, unless you absolutely trust the source to provide you with predictably sized image data that comfortably fits within the available memory."


Solution

  • Sometimes images are to big for bitmaps, so a way to get Your bitmap is to scale down the image. At this site is a good example and it worked very good for me

    http://twigstechtips.blogspot.com/2011/10/android-resize-bitmap-image-while.html