OutofMemoryError: bitmap size exceeds VM budget (Android)

后端 未结 9 730
甜味超标
甜味超标 2020-11-27 18:48

Getting an Exception in the BitmapFactory. Not sure what is the issue. (Well I can guess the issue, but not sure why its happening)

ERROR/AndroidRuntime(7906): jav         


        
相关标签:
9条回答
  • 2020-11-27 19:20

    With reference to this link, please note the outOfMemory error can be solved by the following way:

    public Bitmap decodeFile(String filePath) {
    
    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inPurgeable = true;
    
    try {
    BitmapFactory.Options.class.getField("inNativeAlloc").setBoolean(options,true);
    
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    
    if(filePath != null)
    {
        bitmap = BitmapFactory.decodeFile(filePath, options);               
    }
    
    return bitmap;
    }
    
    0 讨论(0)
  • 2020-11-27 19:20

    I got this error when i began to resize an image from 320x240 to something like 64x240 (downscale), then import into my project (since i wanted to improve rendering speed and it contained a lot of useless alpha regions until this point).

    now the last answer makes much sense:

    You can "expand" your heap by adding this static { @SuppressWarnings("unused") byte dummy[] = new byte[ 8*1024*1024 ]; } to your code, to force the heap to expand. It may make it a bit less frequent.

    I think this is what happened to me. Android automatically decodes drawables into bitmaps, (and then get stored on a heap, all on compilation time?)

    i began seeing the error, when i used the smaller version of my image in runtime (i scale them up on runtime since i program a VGA-game with retro graphics, using BitmapFactory.decodeResource and Bitmap.createScaledBitmap).

    it must be like Marve said: The Heap is not big enough in my case after shrinking my drawable/image and import it into my project.

    I was able to get rid of my OutOfMemoryException when resizing the image back to a bigger size (320x240), which verifies the problem i guess?

    0 讨论(0)
  • 2020-11-27 19:22

    I think it is - what it it say it is. Your image is too big and since it is loaded in the stream when the memory is exhausted the exception is thrown. It's not even the question on how much memory you have overall but how much your particular Activity has available.

    0 讨论(0)
提交回复
热议问题