Can't get Intent.extras () from camera after taking photo

I found this to be a common problem with capturing a photo and getting a full size photo instead of a thumbnail (according to: http://developer.android.com/training/camera/photobasics.html ). Taking a photo and capturing a thumbnail is straightforward, but the rest of the tutorial seems unfinished and doesn't work. Has anyone resolved this in an easy way?

public class TakePhoto extends Activity{

static final int REQUEST_IMAGE_CAPTURE = 1;
static final int REQUEST_TAKE_PHOTO = 1;
private String mCurrentPhotoPath;
private ImageView mImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.take_photo);

    mImageView = (ImageView) findViewById(R.id.imageView);

    dispatchTakePictureIntent();

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            Log.i("ASD", ex.toString());
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

      

}

04-25 18:01:14.239  17281-17281/com.(package).bazaar E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.(package).bazaar, PID: 17281
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.(package).bazaar/com.(package).bazaar.TakePhoto}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3626)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3669)
        at android.app.ActivityThread.access$1300(ActivityThread.java:148)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at com.klangstudios.bazaar.TakePhoto.onActivityResult(TakePhoto.java:46)
        at android.app.Activity.dispatchActivityResult(Activity.java:6161)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3622)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3669)
            at android.app.ActivityThread.access$1300(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

      

+4


source to share


3 answers


I would not have expected it to data

be null

. However, Intent

there shouldn't be any additional functionality on this . You only look "data"

if you haven't specified EXTRA_OUTPUT

. If you specify EXTRA_OUTPUT

, you will receive a photo from the path that you specified in EXTRA_OUTPUT

, and you will ignore the one Intent

that is placed on onActivityResult()

.



As far null

data

Intent

as it goes , it might be something special to the camera app you're using. Please keep in mind that use ACTION_IMAGE_CAPTURE

means that you are relying on a third party application to take a picture, and third party applications may have bugs.

+7


source


This might help: Android camera target: how to get a full size photo?



Although what you are doing is reading the file you created before triggering the camera intent, not reading data

0


source


I wrestled for hours with the same problem as "jean d'arme" except that I was trying to get the URI and not the Bitmap. To save other users from this waste of time, I will highlight the explanations "jean d'arme" and "Felipe Augusto" that helped me fix the error with the following steps:

1) Add a global variable to your class

private Uri uri;

      

2) Use this variable to get content of photoURI inside dispatchTakePictureIntent ()

private void dispatchTakePictureIntent() {

        {...}

        if (photoFile != null) {
            uri = photoURI;
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

      

3) Use this Uri in onActivityResult method for whatever task you are counting on

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        //Perform any task using uri
        //For example set this URI to fill an ImageView like below
        this.imageView.setImageURI(uri);
    }
}

      

I hope this twist helps some of you!

0


source







All Articles