android Image view out of memory error

后端 未结 5 1691
一整个雨季
一整个雨季 2021-01-25 06:43

In my Android project I have imageButton , and after clicking on it , it must open new Activity with imageView , and in my new Activity I must see the ImageButton\'s image only

5条回答
  •  一个人的身影
    2021-01-25 07:30

    I have had some problems with Images and OutOfMemory exceptions, and all of them are obviously caused by the fact that I use too much memory that is assigned for the app (called heap).

    Like I can see in your code, you create an image every time you push the button, and like you said, if the Image has 17M of size, probably if your device is low quality, it has 20M of max heap for each app, so you are out of memory with two images.

    Maybe you can create an image only one time, if you must create more than one image, try to remove previous images, and try to call System.gc(), it could be helpful.

    If you really need to create more than one image, more than one time, you can build a reduced instance of image, setting inSampleSize option before you create the image. If you put a value of 2 to this attribute, you will get a 1/2 image of the original, with reduced quality and size.

    Something like this:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2; // Or other value that you estimate
    

    Then create the image with these options.

    PS: It's not necessary to call super.onCreate() more than once.

提交回复
热议问题