How to overcome this error:java.lang.OutOfMemoryError: bitmap size exceeds VM budget

后端 未结 3 511
傲寒
傲寒 2020-12-17 05:08

I am trying to add images in Sqlite DB and listing the images from DB to listview.... I am storing the image path to get image. When I list the images from DB in device I am

相关标签:
3条回答
  • 2020-12-17 05:17

    see this question

    bitmap size exceeds VM budget

    0 讨论(0)
  • 2020-12-17 05:21

    Have yout tried using one class-wide Bitmap and re-allocate it in your case clause?

    0 讨论(0)
  • 2020-12-17 05:22

    1) try to decode the image bound first

    you get the actual size of the image to prevent the OOM issue. Dont ever decode the image first!!!

        BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    
    BitmapFactory.decodeByteArray(bitmapByteArray, 0, bitmapByteArray.length, options);'
    

    options.outWidth and options.outHeight are what you want.

    2) compute a sample size

    by using the following code from http://hi-android.info/src/com/android/camera/Util.java.html. If you detect the outWidth and outHeight are too big to have OOM issue, just set the outWidth and outHeight to a smaller size. It will give you a sample size that the decoded image will be set to those outWidth and outHeight. The bitmap options here is the same you use in step 1.

        private static int computeInitialSampleSize(BitmapFactory.Options options,
            int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
    
        int lowerBound = (maxNumOfPixels == IImage.UNCONSTRAINED) ? 1 :
                (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == IImage.UNCONSTRAINED) ? 128 :
                (int) Math.min(Math.floor(w / minSideLength),
                Math.floor(h / minSideLength));
    
        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }
    
        if ((maxNumOfPixels == IImage.UNCONSTRAINED) &&
                (minSideLength == IImage.UNCONSTRAINED)) {
            return 1;
        } else if (minSideLength == IImage.UNCONSTRAINED) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
    

    3) Use the computed sample size

    Once you get the sample size, use it to decode the real image data.

                options.inTempStorage = new byte[16*1024];
            options.inPreferredConfig = (config == null)?BitmapUtil.DEFAULT_CONFIG:config;
            options.inSampleSize = BitmapUtil.computeSampleSize(bitmapWidth, bitmapHeight, bitmapWidth < bitmapHeight?targetHeight:targetWidth, bitmapWidth < bitmapHeight?targetWidth:targetHeight, 1);
            options.inPurgeable = true;  
            options.inInputShareable = true;  
            options.inJustDecodeBounds = false;
            options.inDither = true;
            result = BitmapFactory.decodeByteArray(bitmapByteArray, 0, bitmapByteArray.length, options);
    

    ~If this still give you OOM issue, you can try to lower the outWidth and outHeight. bitmap is using native heap, not the java heap. So it is hard to detect how much memory left for the new image decoded.


    ~~If you have to set the outWidth and outHeight too low, then you may have memory leak somewhere in your code. try to release any object from memory that you dont use. e.g bitmap.release();


    ~~~the above is just a sample code. adjust what you need.

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