17

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. 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?

Fattie
  • 27,874
  • 70
  • 431
  • 719
Anton
  • 921
  • 3
  • 12
  • 26
  • 1
    I had the same problem, check out this link: [http://tutorials-android.blogspot.co.il/2011/11/outofmemory-exception-when-decoding.html](http://tutorials-android.blogspot.co.il/2011/11/outofmemory-exception-when-decoding.html) – zwebie Aug 02 '12 at 13:25
  • 1
    Also see Chet Haase's nice tutorial on bitmap scaling: http://www.youtube.com/watch?v=12cB7gnL6po – Peter Tran Apr 25 '13 at 01:25
  • 2
    be aware ***THIS PAGE IS VERY OUT OF DATE*** - today you just do this: http://stackoverflow.com/a/24135522/294884 – Fattie Jul 11 '16 at 16:22
  • have you got answer for this?? Please share code – Jyoti Sharma May 15 '18 at 04:39

4 Answers4

5

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;
    }
Sayan Mukherjee
  • 352
  • 2
  • 21
1

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

Shayden117
  • 252
  • 1
  • 3
  • 17
1

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);
}
  • 1
    did you really try this function yourself? I tried this and function decodeSampledBitmapFromUri always returns null. and after quite long research I found that you cannot use the same inputstream as parameter to BitmapFactory.decodeStream more than once. – Dika Nov 13 '17 at 14:16
  • Just came to this same realization. The inputstream points to the end of the file after the first call. There might be a way to reset it rather than create a new one... – hippietrail Sep 08 '19 at 10:57
0

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).

Peter O
  • 19
  • 3
  • 1
    Sounds like a good approach, code for it would make better answer – weston Apr 28 '16 at 11:04
  • @weston Google provides this sample code (regarding efficiently loading bitmaps) at [link](http://developer.android.com/training/displaying-bitmaps/load-bitmap.html) The use of the ContentResolver can be found by looking at the implementation for MediaStore.getBitmap. It makes a call to the content resolver in the same way – Peter O May 05 '16 at 14:14
  • 2
    I know you can find the answers elsewhere, as I had to do this already because the answer is incomplete. Everyone who reads this answer will have to do the same leg work. A good Stack Overflow answer does not rely on external links to complete it. – weston May 05 '16 at 14:46