Android Error java.lang.OutOfMemoryError: bitmap size exceeds VM budget

前端 未结 1 339
夕颜
夕颜 2021-01-29 06:12

Am getting Exception error when loading images from assets to arraylist. this is the error in log cat: E/AndroidRuntime(2837): java.lang.OutOfMemoryError: bitmap si

相关标签:
1条回答
  • Once, i tried to re-size the image, i' also having the same problem, i used the below code, you can modify as it yours,

    public Bitmap custom_SizedImage(String intent_data2) {
    
            Options options = new Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(intent_data2, options);
            double sampleSize = 0;
            Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math
                    .abs(options.outWidth - targetWidth);
    
            if (options.outHeight * options.outWidth * 2 >= 1638) {
                sampleSize = scaleByHeight ? options.outHeight / targetHeight
                        : options.outWidth / targetWidth;
                sampleSize = (int) Math.pow(2d,
                        Math.floor(Math.log(sampleSize) / Math.log(2d)));
            }
            options.inJustDecodeBounds = false;
            options.inTempStorage = new byte[128];
            while (true) {
                try {
                    options.inSampleSize = (int) sampleSize;
                               // here you can do you Decode process
                    //mBitmap = BitmapFactory.decodeFile(intent_data2, options);
    
                    break;
                } catch (Exception ex) {
                    try {
                        sampleSize = sampleSize * 2;
                    } catch (Exception ex1) {
    
                    }
                }
            }
            return scaledBitmap;
    
        }
    
    0 讨论(0)
提交回复
热议问题