java.lang.OutOfMemoryError - BitmapFactory.decode(strPath)

后端 未结 5 1232
[愿得一人]
[愿得一人] 2020-11-29 22:21

I am getting java.lang.OutOfMemoryError, whenever i am calling UploadActivity.java

Line Number 176 is:

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

    It's an optimisation problem for your application. You are getting OutOfMemoryError because when you are doing BitmapFactory.decodeFile(strPath) android is trying to allocate memory for that bitmap. In your case system can't find enough free space to allocate and that's why you are getting this error.

    Now as i can see from your code you are trying to show list of images using ImageAdapter. In that case your imageview must have smaller width and height than actual image.

    To give you a more generalised idea here's what is happening:

    • ImageView width * height = 100dp * 100dp
    • Image width * height = 800px * 800px.

    In this scenario though our imageview width-height is 100 * 100 but we are trying to set 800*800 image as it's background. And this definitely is not an efficient way because the system will allocate memory for 800*800px image whereas 100*100 would do.

    That's why before you do any decoding of bitmap you should sample the bitmap so that only 100*100 worth of memory is allocated.

    You will find a more detail version of this explanation here

提交回复
热议问题