OutOfMemory exception when loading bitmap from external storage

前端 未结 13 1717
广开言路
广开言路 2020-11-29 06:52

In my application I load a couple of images from JPEG and PNG files. When I place all those files into assets directory and load it in this way, everything is ok:

         


        
13条回答
  •  一向
    一向 (楼主)
    2020-11-29 07:16

    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.

提交回复
热议问题