MediaStore.Images.Media.getBitmap 和内存不足错误 [英] MediaStore.Images.Media.getBitmap and out of memory error

查看:52
本文介绍了MediaStore.Images.Media.getBitmap 和内存不足错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码代码是:

public Bitmap loadPhoto(Uri uri) {
    Bitmap scaled = null;
    try {
    scalled = Bitmap.createBitmap(
      MediaStore.Images.Media.getBitmap(getContentResolver(), uri),
      0,0,90, 90);

    if (scaled == null) { return null; }
    } catch(Exception e) { }
    return scaled;
}

在这之后.我在 ImageView 中显示缩放.每张图片都来自设备摄像头.

After this. I display scaled in ImageView. Every image comes from the device camera.

每次显示三张相机照片后,都会出现错误:内存不足.如何解决这个问题?

Every time, I get error: out of memory after I display three photos from camera. How to solve this?

推荐答案

Praveen Katha 的答案将始终返回 null.这是更新后的答案.

Answer of Praveen Katha will always return null. Here is the updated answer.

这是诀窍,每次使用后关闭输入流.Input Stream 表示使用一次.如需更多信息,请关注此答案

Here is the trick, close the input stream after every use. Input Stream means to be used one time. For more information, please follow this answer

private 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) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) >= reqHeight
                    && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

    public static Bitmap decodeSampledBitmapFromUri(Context context, Uri imageUri, int reqWidth, int reqHeight) throws FileNotFoundException {
        Bitmap bitmap = null;
        try {
            // Get input stream of the image
            final BitmapFactory.Options options = new BitmapFactory.Options();
            InputStream iStream = context.getContentResolver().openInputStream(imageUri);

            // First decode with inJustDecodeBounds=true to check dimensions
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(iStream, null, options);
            if (iStream != null) {
                iStream.close();
            }
            iStream = context.getContentResolver().openInputStream(imageUri);

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

            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeStream(iStream, null, options);
            if (iStream != null) {
                iStream.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

这篇关于MediaStore.Images.Media.getBitmap 和内存不足错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆