OutOfMemory exception when loading bitmap from external storage

I tried all the approaches mentioned here & at other resources but I came to the inference that setting ImageView’s reference to null will solve the issue:

  public Bitmap getimage(String path ,ImageView iv)
   {
    //iv is passed to set it null to remove it from external memory
    iv=null;
    InputStream stream = new FileInputStream("/mnt/sdcard/mydata/" + path);
    Bitmap bitmap = BitmapFactory.decodeStream(stream, null, null);
    stream.close();
    stream=null;
    return bitmap;
    }

& you are done!

Note:Though it may solve above problem but I would suggest you to check Tom van Zummeren ‘s optimized image loading.

And also check SoftReference: All SoftReferences pointing to softly reachable objects are guaranteed to be cleared before the VM will throw an OutOfMemoryError.

Leave a Comment