6

I am trying to capture an image from an existing camera application, save the image in a customized folder, and display the thumbnail in and imageView. The camera supplies the thumbnail as long as I haven't specified where to save the file:

I can get the thumbnail from the returned intent:

...
  Intent i = = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  startActivityForResult(i)
}

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  super.onActivityResult(requestCode, resultCode, intent);      
  Bundle extras = intent.getExtras();
  Bitmap mImageBitmap = (Bitmap) extras.get("data");
}

Or I can save the file in a specified folder (which works fine)

  ...
  Intent i = = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  i.putExtra((MediaStore.EXTRA_OUTPUT, outputFileUri);
  startActivityForResult(i)
}

but the thumbnail is no longer stored in the intent extra "data", and when I try to retrieve the thumbnail, I get an error (this is from my LogCat)

10-04 06:30:14.463: E/AndroidRuntime(1967): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity: java.lang.NullPointerException

As you can see, the field returned is null instead of the bitmap thumbnail. I have tried decoding the bitmap afterwards to generate a thumbnail from the file directly, but it takes too long (even when downsampled I get out of memory error) , and it seems counterintuitive to do the job twice. Any suggestions?

James Wiseman
  • 29,946
  • 17
  • 95
  • 158
keighty
  • 362
  • 1
  • 3
  • 11
  • I think [this answer][1] can help you for specifing file directory. [1]: http://stackoverflow.com/a/9734268/1172945 – Tugrul Oct 04 '12 at 11:12

3 Answers3

5

Okay. If you are passing an outputURI to the intent then you will not be able to receive the data back from the intent in onActivityResult().

I think only option is to use the same outputURI to display the thumbnail..

Try this.

void captureImage(){
    File file = new File(Environment.getExternalStorageDirectory()
    .getAbsolutePath() + "/MyFolder", "myImage"+ ".jpg");

    mCapturedImagePath = file.getAbsolutePath();
    Uri outputFileUri = Uri.fromFile(file);
    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(i, CAMERA_REQUEST);
}

onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST) {
        if (resultCode == RESULT_OK) {
            File file = new File(mCapturedImagePath);
            imageView.setImageURI(Uri.fromFile(file));
        }
    }
}
Kamalesh M. Talaviya
  • 1,422
  • 1
  • 12
  • 26
Purush Pawar
  • 4,263
  • 2
  • 29
  • 37
  • Thanks for your suggestion, but I am testing the result of the returned intent within the same activity (in onActivityResult, actually). The camera does not return a thumbnail if I have given a i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); – keighty Oct 04 '12 at 11:36
  • camera_request is a integer request code and give it any value you want. e.g CAMERA_REQUEST = 1 – Purush Pawar Oct 04 '12 at 12:14
  • I think you are correct -- I was trying to have my cake an eat it too! Once the output file has been specified, the camera Activity does not return an intent, which explains why the "data" field was null. Thanks for your perseverance – keighty Oct 04 '12 at 12:19
0

Your Bitmap mImageBitmap is a local variable, make that global if you want to use it outside the onActivityResultFunction otherwise set the image there as

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST) {
        photo = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(photo);
    }
}
Afnan
  • 888
  • 1
  • 13
  • 37
Aamir Shah
  • 4,473
  • 8
  • 21
  • 28
  • thanks for the reply, but the problem isn't that I can't use the bitmap elsewhere, it is that the camera doesn't return the thumbnail when I give it an output file name – keighty Oct 04 '12 at 11:33
  • try this Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST); – Aamir Shah Oct 04 '12 at 11:34
  • CAMERA_REQUEST is an integer returned from the Camera. Why start the camera application with another intent when it has just returned? – keighty Oct 04 '12 at 11:43
  • I am happy to try it, but what is the value of CAMERA_REQUEST? – keighty Oct 04 '12 at 12:07
  • this solution wont work if MediaStore.EXTRA_OUTPUT is used as an intent extra. – Chris Aug 18 '14 at 09:57
-1

try this

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
Aamir Shah
  • 4,473
  • 8
  • 21
  • 28