-1

I am trying to load bitmap image from file in some devices my code is working perfectly but on the other device it gives OutOfMemory exception.

String filePath  = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));

fileName.add(filePath);

File file = new File(filePath);
Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
bitmapArray.add(myBitmap);
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
qasim butt
  • 133
  • 11
  • 3
    Possible duplicate of [Strange out of memory issue while loading an image to a Bitmap object](https://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – ADM Nov 15 '17 at 07:00
  • Possible duplicate of [Out of memory android issue](https://stackoverflow.com/questions/19402072/out-of-memory-android-issue) – JoxTraex Nov 15 '17 at 07:19

1 Answers1

0

You can find out the size of the bitmap:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;

and then compress it to the desired size by setting sampleSize:

options = new BitmapFactory.Options();
options.inSampleSize = desired_scale_value;
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);

And don`t forget to load bitmaps in non ui thread.

Alex Nik
  • 753
  • 6
  • 15