Skip to content Skip to sidebar Skip to footer

Java.lang.outofmemory Error At Bitmap Factory.decodefile In Android

First i call MediaStore.ACTION_IMAGE_CAPTURE intent to open camera then i get the saved captured image path from this function. private String getLastImageId(){ String[] imageC

Solution 1:

if(imgFile.exists()){

    final BitmapFactory.Optionsoptions=newBitmapFactory.Options();
    options.inSampleSize = 8;

   BitmapmyBitmap= BitmapFactory.decodeFile(imgFile.getAbsolutePath(),options);
        holder.imgIcon.setImageBitmap(myBitmap); 
        //myBitmap.recycle();
    }

Use inSampleSize to load scales bitmaps to memory. Using powers of 2 for inSampleSize values is faster and more efficient for the decoder. However, if you plan to cache the resized versions in memory or on disk, it’s usually still worth decoding to the most appropriate image dimensions to save space.

For more see Loading Large Bitmaps Efficiently

Solution 2:

You will run into out of memory issues if you try to decode big images yourself.

Try using Universal Image loader. It will take care of all image loading from local storage or the internet.

https://github.com/nostra13/Android-Universal-Image-Loader

Solution 3:

OutOfMemory can be tiring here are some few options that you can look

try { 
     //Create your bitmap here 

} catch (OutOfMemoryError ooM) {
        // You got out of memory now do something to recycle images// Yes you can catch OOM.recycle();
}

If you do not have many images to handle then you can try this.

    BitmapFactory.Optionsop=newBitmapFactory.Options();
    op.inSampleSize = 8;
    return BitmapFactory.decodeFile("<YOUR IMAGE FILE HERE>", op);

A combination of above can be useful if you have many images

Post a Comment for "Java.lang.outofmemory Error At Bitmap Factory.decodefile In Android"