OutofMemoryError: bitmap size exceeds VM budget (Android)

后端 未结 9 759
甜味超标
甜味超标 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;
    }
    

提交回复
热议问题