MediaStore.Images.Media.getBitmap and out of memory error

后端 未结 4 867
情歌与酒
情歌与酒 2020-12-08 22:39

My code code is:

public Bitmap loadPhoto(Uri uri) {
    Bitmap scaled = null;
    try {
    scalled = Bitmap.createBitmap(
      MediaStore.Images.Media.getB         


        
相关标签:
4条回答
  • 2020-12-08 23:02

    Try using BitmapFactory to fix the problem http://developer.android.com/reference/android/graphics/BitmapFactory.html

    0 讨论(0)
  • 2020-12-08 23:05

    Answer of Praveen Katha will always return 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;
        }
    
    0 讨论(0)
  • 2020-12-08 23:08

    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);
    }
    
    0 讨论(0)
  • 2020-12-08 23:15

    The MediaStore.getBitmap method is a convenience method that does not specify a sample size when obtaining the bitmap. 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).

    0 讨论(0)
提交回复
热议问题