1

I'm working with camera and gallery . I've code that get image from gallery and returns to onactivityresult .

when user choose image from gallery and goes to on activity result ,I get outofmemeory error on this line :

thePic = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());

where am I doing is wrong ?

navid joe
  • 59
  • 6
  • Possible duplicate of [java.lang.OutOfMemoryError in android while getting image from gallery in android](http://stackoverflow.com/questions/14051068/java-lang-outofmemoryerror-in-android-while-getting-image-from-gallery-in-androi) – Beloo Oct 16 '16 at 07:45

3 Answers3

0

Loading image directly from gallary can kill the app . we should never do that we should first scale that image on our requirement and than should use that scaled image some times app can not handle big bitmap in memmory

so below code will decode your image in required size just give sizeOfImage to your requred size It will also manage your arspect ratio of image

Put below code in your onactivityresult

Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(
        selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();

Bitmap mm = decodeSampledBitmapFromResource(new File(filePath),sizeOfImage,sizeOfImage);

above code will use below function so also put below function at somewhere

    public static Bitmap decodeSampledBitmapFromResource(File res, int reqWidth, int reqHeight) {

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

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

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(res.getAbsolutePath(),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) {

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

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

You should change this function as per your requirement and also refer following link https://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Miral Bhalani
  • 274
  • 2
  • 9
0

The best practice to load bitmaps in android is to find the bitmap dimensions before you load it. Then loading a suitable size of the bitmap depend on your device memory size and screen size.

I highly recommend you to read this great tutorial in develope.android website. this will help you to get a complete sense about how bitmaps works and whats the best practice to load them also source code is included. please go through all these tutorials.

https://developer.android.com/training/displaying-bitmaps/index.html

https://developer.android.com/training/displaying-bitmaps/load-bitmap.html

https://developer.android.com/training/displaying-bitmaps/process-bitmap.html

https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

https://developer.android.com/training/displaying-bitmaps/manage-memory.html

https://developer.android.com/training/displaying-bitmaps/display-bitmap.html

I suggest you to read these links but if you nor interested you can use libraries like Glide or Picasso for loading your bitmap. they will help you to load without an OutOfMemory Error.

madhan kumar
  • 1,560
  • 2
  • 26
  • 36
Amir Ziarati
  • 14,248
  • 11
  • 47
  • 52
-1

Reason: This happens when your app exceeds the allocated heap memory. The image can be too large to fit in the heap.Before handling the image you have to scale down the bitmap. Please, go through this link for proper fix Avoiding memory leaks.

For a quick fix:

Increase heap size of your app. By, put the below line into your manifest.xml file.

<application
     android:largeHeap="true" >

Note: This is not a best practice.

Check out document that can help you - Loading Large Bitmaps Efficiently

madhan kumar
  • 1,560
  • 2
  • 26
  • 36