2

Hi I am using this piece of code for camera in my activity.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == TAKE_PICTURE_WITH_CAMERA) {
        if (resultCode == Activity.RESULT_OK) {

            // To Write the Images in SDCard
            File f = new File(imgName);
            try {
                Uri capturedImage = Uri
                                 .parse(android.provider.MediaStore.Images.Media
                                     .insertImage(getContentResolver(),
                                     f.getAbsolutePath(), null, null));

                Log.i("camera",
                      "Selected image: " + capturedImage.toString());

                pic.setImageURI(capturedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            Log.i("Camera", "Result code was " + resultCode);
        }
    }
}

I am trying to apply the image captured to my ImageView. The first time, the camera takes a picture and sets it to the ImageView. But when I run it for the second time, my app is not able to set the newly captured image to the imageView. It crashes after returning from the camera intent. How can I solve this? Any help is appreciated.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • What does the LogCat say when it crashes? – forsvarir May 24 '11 at 06:58
  • @forsvarir Well, the problem is, Its working fine in emulator, but the problem is when I test it in device. – Andro Selva May 24 '11 at 07:27
  • If you connect your device up to Eclipse (I'm assuming you're using that), you should be able to select it as the target device and debug the deployed application, getting LogCat results etc, as if it was running on the emulator. – forsvarir May 24 '11 at 07:29

2 Answers2

5

In order to avoid this kind of error, the bitmap has to be scaled first. I have tried the below piece of code and it works fine.

bm1=Bitmap.createScaledBitmap(bm, 300, 300,true);
                    pic.setImageBitmap(bm1);

                    bm.recycle();
                    bm=null;
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • from what I see of your above code you're working with URI's. I'm encountering the same issue but my OOM error occurs at the Uri capturedImage = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), f.getAbsolutePath(), null, null)); line. Can you comment on how you got around that? – chris-tulip Nov 06 '12 at 22:00
  • 1
    This worked well for me, thanks. It wasn't the scaling but the use of `recycle()` since AFAICS the bitmap wasn't garbage collected before I generated the next one. – darrenp Feb 21 '13 at 10:32
-3

When reading about Camera, I think it is Hardware. Hardware always is using its own Thread. Perhaps you need to create a memory copyied image and serve it by dispatcher. I do not know your code but I am interested in receiving it to help you.

http://blog.lieberlieber.com/2009/07/09/controlinvoke-wpf-style-and-special-bitmapsource-handling/

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f0c5e590-8bfe-4452-a784-4987af9fce89/

goldengel
  • 611
  • 8
  • 22