Kelib
Kelib

Reputation: 648

Unable to save a photo captured with the native camera app

I'm trying to use the built-in camera app, calling it via an intent from my own app, to take a picture at a specified place. I've use lot of sample code from developer.android.com but it throws an exception:

java.lang.RuntimException: Failure delivering result ResultInfro(who=null, request=1, result=1, data=null). 

I check the SDcard but no image is created. I've read lot of threads here, but none of them helped me.

My code:

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraApp");

// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()) {
    if (! mediaStorageDir.mkdirs()) {
        Log.d("MyCameraApp", "failed to create directory");
        return null;
    }
}

// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                     "IMG_"+ timeStamp + ".jpg");
Uri fileUri =  Uri.fromFile(getOutputMediaFile());
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, TAKE_PICTURE);

And what I do next:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    //taken photo
    case TAKE_PICTURE:
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Image saved to:\n" +
                    data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }
}

Upvotes: 0

Views: 788

Answers (1)

Rémi
Rémi

Reputation: 3744

This answers a very similar question: I'm getting a NullPointerException when I use ACTION_IMAGE_CAPTURE to take a picture

Simplest solution is to keep an instance variable with the file name at the time when you start the activity, and load the file name from that instance variable during onActivityResult.

Upvotes: 1

Related Questions