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