从文件加载图像时出现OutOfMemoryError

时间:2014-12-10 19:29:11

标签: android

我知道这个question,从我所知道的情况来看,我实际上做的与最佳答案建议的一样。但是我仍然在内存较少的设备上遇到outOfMemory崩溃。

编辑:我只是用手机原生相机拍照。没什么好的。

private Bitmap setImgViewFromFile(ImageView imgView, String file) {
    // Get the dimensions of the View
    int targetW = Math.max(imgView.getWidth(), 600);
    int targetH = Math.max(imgView.getHeight(), 800);


    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(file, bmOptions);
    imgView.setImageBitmap(bitmap);
    return bitmap;
}

Stacktrace:

12-10 10:38:04.120    4144-4144/com.loop E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.loop, PID: 4144
java.lang.OutOfMemoryError
        at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
        at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:613)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:589)
        at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:369)

谁能看到我做错了什么?

0 个答案:

没有答案