0

I trying a gallery app. My app take a photo or choose a picture in gallery. when I wanted to set width and height the selected image. I give OutOfMemoryError.

This set picture function, This function use one parameter, picture path.

private void setPic(String mCurrentPhotoPath) {


            targetW         = foto_image.getWidth();
            targetH         = foto_image.getHeight();


        // Get the dimensions of the bitmap
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;

        // Determine how much to scale down the image
        int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

        // Decode the image file into a Bitmap sized to fill the View
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;

            Bitmap bitmap       = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
            String uri_path     = storeImage(bitmap);

            String respond      = crud.update_photo_path(String.valueOf(position),uri_path);
            if(respond == "error") {
                Toast.makeText(getActivity(),getResources().getString(R.string.photos_sqlite_error),Toast.LENGTH_LONG).show();
                return;
            }

            foto_image.setImageBitmap(bitmap);
    }

this is error

11-28 15:24:28.385    3248-3248/com.tupbebekailesi.medyasef.hamilelikrehberi E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:530)
            at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:303)
            at com.tupbebekailesi.medyasef.hamilelikrehberi.PhotosActivity.setPic(PhotosActivity.java:251)
            at com.tupbebekailesi.medyasef.hamilelikrehberi.PhotosActivity.photo_cam_image(PhotosActivity.java:164)
            at com.tupbebekailesi.medyasef.hamilelikrehberi.PhotosActivity.onActivityResult(PhotosActivity.java:116)
            at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:166)
            at android.app.Activity.dispatchActivityResult(Activity.java:5322)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3491)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3538)
            at android.app.ActivityThread.access$1100(ActivityThread.java:153)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
            at dalvik.system.NativeStart.main(Native Method)

3 Answers3

0

This is a very tricky concept in Android, you might want to look into

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

It is highly important that we sample the image based on the size it is going to fit into, you can find all the related detils in the above link.

Also very important to re-use the image containers. Try and carry bitmap images in minumum views.

As each applition is given a specified amount of heap memory to store it's cached values, like 16, 32 64Mb, it hold good for data, when dealing with bitmaps, it does not get garbage collected (non bitmap objects do get gc-ed), hence locking up memory and causing OOM

stack_ved
  • 721
  • 3
  • 11
0

Working with bitmaps in android is tricky. In your case picture may exceed vm budget. Try making a buffer and processing image through fixed size buffer. It may be slower, but you will secure your code from OutOfMemory exception.

This link may help you OutOfMemoryError: bitmap size exceeds VM budget :- Android

Community
  • 1
  • 1
Roman Bugaian
  • 354
  • 1
  • 7
  • 22
0

The link posted by stack_ved explains the issue.

See also this question/answer

Community
  • 1
  • 1
MarcoS
  • 17,323
  • 24
  • 96
  • 174