OutofMemoryError: bitmap size exceeds VM budget (Android)

后端 未结 9 729
甜味超标
甜味超标 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 18:59

    I ended up resizing the bitmap using the following code which seems to have resolved the issue.

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;
    Bitmap preview_bitmap = BitmapFactory.decodeFile(mPathName, options);
    
    0 讨论(0)
  • 2020-11-27 18:59

    Have you checked the DDMS? With what I have been encountering, it's probably not the size of the images, because Android seems to handle large images pretty well. If you trace the heap with DDMS you may find that you happen to have a lot of free memory. 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. Unfortunately, with the exception it claims that it can't allocate some amount of bytes. Say 1M. If you take a look at the "free" line you will see that the largest block is >> 1M. There is something strange there that I can't figure out. It is not related even to the speed of swiping images. I saw in some thread that you can call "recycle" or so for bitmaps. I still don't see why it should help if the heap size is way above the taken size.

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

    inSampleSize is a good hint. But a fixed value often doesn't work fine, since large bitmaps from files usually are user files, which can vary from tiny thumbnails to 12MP images from the digicam.

    Here's a quick and dirty loading routine. I know there's room for improvement, like a nicer coded loop, using powers of 2 for faster decoding, and so on. But it's a working start...

    public static Bitmap loadResizedBitmap( String filename, int width, int height, boolean exact ) {
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile( filename, options );
        if ( options.outHeight > 0 && options.outWidth > 0 ) {
            options.inJustDecodeBounds = false;
            options.inSampleSize = 2;
            while (    options.outWidth  / options.inSampleSize > width
                    && options.outHeight / options.inSampleSize > height ) {
                options.inSampleSize++;
            }
            options.inSampleSize--;
    
            bitmap = BitmapFactory.decodeFile( filename, options );
            if ( bitmap != null && exact ) {
                bitmap = Bitmap.createScaledBitmap( bitmap, width, height, false );
            }
        }
        return bitmap;
    }
    

    Btw, in the newer APIs there are also lots of BitmapFactory.Option's for fitting the image to screen DPIs, but I'm not sure whether they really simplify anything. Using android.util.DisplayMetrics.density or simply a fixed size for less memory consumption seem to work better imho.

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

    use these options in decodefile. hope u can elemenate bitmap exceeds vm budget problem..

    BitmapFactory.Options bfOptions=new BitmapFactory.Options(); 
    
    bfOptions.inDither=false;          //Disable Dithering mode
    bfOptions.inPurgeable=true;       //Tell to gc that whether it needs free memory, the Bitmap can be cleared
    bfOptions.inInputShareable=true;  //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
    bfOptions.inTempStorage=new byte[32 * 1024]; 
    
    0 讨论(0)
  • 2020-11-27 19:12

    http://code.google.com/p/android/issues/detail?id=8488

    http://mobi-solutions.blogspot.com/2010/08/how-to-if-you-want-to-create-and.html

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

    Make sure to guard your bitmap creation from out of memory errors! With most platforms, android doesn't have much memory to play with and it runs out quickly with bitmaps. Also, make sure to manually recycle your bitmaps as much as possible, I've noticed that the garbage collection can be rather slow.

    try{            
      Bitmap myFragileBitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
    }
    catch(IllegalArgumentException e){
      Log.e(TAG,"Illegal argument exception.");
    }
    catch(OutOfMemoryError e){
      Log.e(TAG,"Out of memory error :(");
    }
    
    0 讨论(0)
提交回复
热议问题