0

在我的应用程序中,我需要缩放图像,这很正常。但是当加载大图时,decodeStream 方法返回 null。目前它在图像大于 6 MB (5616x3744) 时不起作用

有人知道为什么会发生这种情况,我能做些什么来解决这个问题?

public static Bitmap scaleImage(final String filepath, int height, int width) throws IOException{

    if (filepath == null){
        Log.e(TAG, "cannot create Thumbnail without file");
        return null;
    }

    File f = new File(filepath);
    InputStream is = new FileInputStream(f); 
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(is, null, options);
    is.close();
    final int imageHeight = options.outHeight;
    final int imageWidth = options.outWidth;
    String imageType = options.outMimeType;

    final int thumbnailHeight = height;
    final int thumbnailWidth = width;

    int heightRatio = Math.round((float) imageHeight) / Math.round((float) thumbnailHeight);
    int widthRatio = Math.round((float) imageWidth) / Math.round((float) thumbnailWidth);
    int inSampleSize = 0;
    if (heightRatio < widthRatio){
        inSampleSize =  heightRatio ;
    }
    else{
        inSampleSize =  widthRatio;
    }

    options.inSampleSize = inSampleSize;
    // Decode bitmap with inSampleSize set
    int retrySteps = 0;
    InputStream is2 = new FileInputStream(f);
    Bitmap bmp = null;
    for (int i = 0; i < 5; i++){
        options.inJustDecodeBounds = false;
        try {
            bmp = BitmapFactory.decodeStream(is2, null, options);
            if (bmp != null){
                i = 10;             
            }
            else{
                System.gc();    
                SystemClock.sleep(i * 1000);
                options.inSampleSize = inSampleSize + 1;
            }

        } catch (OutOfMemoryError e) {
            System.gc();    
            SystemClock.sleep(i * 1000);

        }

    }
    is2.close();        
    return bmp;

谢谢

4

0 回答 0