2

Avoid outof memory error for selecting large image file from sdcard or from resource. how can i tackle with this problem?

4

3 回答 3

12

To avoid error while selecting file from sd card in bitmap or using setImageURI to sdcard use following method:

public static Bitmap decodeScaledBitmapFromSdCard(String filePath,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filePath, options);
}

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}
于 2013-03-13T04:37:52.950 回答
3

您可以使用 inSampleSize 来减少内存占用。

这是代码

public static Bitmap decodeAndResizeFile(File f) {
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 70;

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}

在这里,它将使用解码图像inSampleSize,此代码将为inSampleSize您找到最佳值。

它对我来说很好。

如果你不想使用上面的代码,你也可以使用bitmap.recycle()System.gc()释放未使用的内存。但是,上面的代码对我来说很好。您可以使用两者中的任何一个。

objbitmap.recycle();
objbitmap = null;
System.gc();

希望,这可以解决问题!

于 2013-03-13T05:01:17.257 回答
2

这取决于您是否知道最终想要的尺寸。

如果你知道尺寸:

请参阅 Android_Craker 的答案。这是解决问题的可靠方法。

如果您不知道尺寸:

在这种情况下,您希望图像与内存一样大。解决方案是循环下采样,直到找到适合的大小: BitmapFactory.Options options = new BitmapFactory.Options();

boolean done = false;
int downsampleBy = 1;
Bitmap bitmap = null;
while (!done) {
    options.inSampleSize = downsampleBy++;
    try {
        bitmap = BitmapFactory.decodeFile(filePath, options);
        done = true;
    catch (OutOfMemoryError e) {
        // Ignore.  Try again.
    }
}
于 2013-03-13T04:50:38.313 回答