5

I am using the default camera intent to get the image in my app. The problem is camera returns null on onActivityResult() . The ResultCode and RequestCode are returning as expected.

My intent call is:

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1224;
....
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

OnactivityResult is:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
    //use imageUri here to access the image
    Uri imageuri = data.getData(); // here is getting crash 
    imageView.setImageFromUri(imageUri);
}
}
}

void setImageFromUri(Uri imgUri){
 ... TODO assign image from uri
}

As I put Log I got the resultCode and responseCode are not null

resultCode = -1
requestCode = 1224

Where I am doing mistake?

But the taken picture is stored in the path (imageUri) as I specified

Is there any other way to get image using camera.

Mahendran
  • 2,719
  • 5
  • 28
  • 50

1 Answers1

10

Seems you know the imageUri before onActivityResult. This is not correct answer but will work fine.

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 

// This image uri only you're going to use

So don't use

 Uri imageuri = data.getData();

just use the uri you known.

your code looks like this:

if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
  if (resultCode == RESULT_OK) {
//use imageUri here to access the image
imageView.setImageFromUri(imageUri); // imageUri should be global in the activity
  }
}
Shadowtech
  • 376
  • 1
  • 4
  • 13
  • 1
    Thanks Shadowtech this is what I did... It's working. :) You know why the intent is null? – Mahendran Dec 13 '11 at 05:59
  • If you remove intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); it then you will get image in intent otherwise Intent will be null and image will be stored on URI... – AZ_ Jul 23 '12 at 12:03