0

I am using camera Intent to take a photo and populate it to ImageView. I got an error whenever I take another photo from the camera Intent. It works well when I am only taking one photo but on second attempt without killing the application, the OutOfMemory Error pops up.

This is the logcat

01-02 09:51:39.930: E/AndroidRuntime(31339): FATAL EXCEPTION: main
01-02 09:51:39.930: E/AndroidRuntime(31339): java.lang.OutOfMemoryError
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:722)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:788)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at com.example.photosharingtest2.MainActivity.onActivityResult(MainActivity.java:218)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.app.Activity.dispatchActivityResult(Activity.java:5390)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3178)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3225)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.app.ActivityThread.access$1100(ActivityThread.java:140)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1275)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.os.Looper.loop(Looper.java:137)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.app.ActivityThread.main(ActivityThread.java:4898)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at java.lang.reflect.Method.invokeNative(Native Method)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at java.lang.reflect.Method.invoke(Method.java:511)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at dalvik.system.NativeStart.main(Native Method)

This is what I did to get the camera to populate photo taken.

btn_takeImage = (Button) findViewById(R.id.btn_takeImage);
    btn_takeImage.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // Intent camera_intent = new
            // Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            // startActivityForResult(camera_intent, CAMERA_PIC_REQUEST);

            values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, "New Picture");
            values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
            imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(intent, PICTURE_RESULT);
        }
    });
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    switch (requestCode)
    {
        case PICTURE_RESULT:
            if (requestCode == PICTURE_RESULT)
                if (resultCode == Activity.RESULT_OK)
                {
                    try
                    {
                        thumbnail = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);

                        img_backgroundImage.setImageBitmap(thumbnail);
                        imageurl = getRealPathFromURI(imageUri);
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            break;
    }
}
twlkyao
  • 14,302
  • 7
  • 27
  • 44
thhVictor
  • 338
  • 6
  • 25

2 Answers2

1

First off the OOM might be caused by a large image, you should look at this documentation to see if you can load a downscaled image instead: Loading Large Bitmaps Efficiently

Secondly is the thumbnail a instance reference? In that case I would suggest you to make it local variable instead (letting it fall out of scope after the method is finished), since I guess you have it hanging around to the second image is taken thus doubling the amount of memory your app holds (since the old reference won't be dereferenced until the new image is set).

In code change:

thumbnail = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
img_backgroundImage.setImageBitmap(thumbnail);
imageurl = getRealPathFromURI(imageUri);

to:

Bitmap thumbnail = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
img_backgroundImage.setImageBitmap(thumbnail);
imageurl = getRealPathFromURI(imageUri);

Magnus
  • 1,483
  • 11
  • 14
0

check out following link that explains the issue you are facing.

http://tutorials-android.blogspot.co.il/2011/11/outofmemory-exception-when-decoding.html

Sathya
  • 140
  • 3