简体   繁体   English

MediaStore.Images.Media.getBitmap和内存不足错误

[英]MediaStore.Images.Media.getBitmap and out of memory error

My code code is: 我的代码是:

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;
}

After this. 在这之后。 I display scaled in ImageView. 我在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? 怎么解决这个?

Answer of Praveen Katha will always return null. Praveen Katha的答案总是会返回null。 Here is the updated answer. 这是更新的答案。

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;
    }

The MediaStore.getBitmap method is a convenience method that does not specify a sample size when obtaining the bitmap. MediaStore.getBitmap方法是一种便捷方法,在获取位图时不指定样本大小。 If you are using getBitmap(ContentResolver, Uri), and want to use a sample size, just use the ContentResolver to get the input stream, and decode the bitmap as you would normally (calculating sample size first, and then loading it with the appropriate sample size). 如果您正在使用getBitmap(ContentResolver,Uri),并且想要使用样本大小,只需使用ContentResolver获取输入流,并像平常一样解码位图(首先计算样本大小,然后使用适当的方法加载它)样本量)。

For those who are looking for code sample: 对于那些正在寻找代码示例的人:

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 {

    // 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);

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeStream(iStream, null, options);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android MediaStore.Images.Media.getBitmap返回错误 - Android MediaStore.Images.Media.getBitmap returns error MediaStore.Images.Media.getBitmap意外失败 - MediaStore.Images.Media.getBitmap fails unexpectedly MediaStore.Images.Media.getBitmap需要太多时间 - MediaStore.Images.Media.getBitmap takes too much time 可以MediaStore.Images.Media.getBitmap(); 在普通的Java类中使用? - can MediaStore.Images.Media.getBitmap(); be use in normal java class? 方法MediaStore.Images.Media.getBitmap(cr,uri); 返回位图大小超出VM预算 - method MediaStore.Images.Media.getBitmap(cr, uri); returns bitmap size exceeds VM budget MediaStore.Images.Media.getBitmap在通知largeicon中返回System.err - MediaStore.Images.Media.getBitmap returns System.err in notification largeicon 如何从 MediaStore.Images.Media.getBitmap(contentResolver, uri) 获取位图? - How to get Bitmap from MediaStore.Images.Media.getBitmap(contentResolver, uri)? MediaStore.Images.Media.insertImage()错误 - MediaStore.Images.Media.insertImage() error 未为类型MediaStore.Audio.Media android定义方法Media.getBitmap(ContentResolver,Uri) - method Media.getBitmap(ContentResolver, Uri) is undefined for type MediaStore.Audio.Media android 内存错误与图像有关 - Out Of Memory Error with images
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM