JGagnon
JGagnon

Reputation: 55

Android ACTION_IMAGE_CAPTURE intent crashes before returning to app

I'm trying to receive the URI of an image taken via the Mediastore.ACTION_IMAGE_CAPTURE intent, but for some reason the default camera app (Google+) crashes before it even gets back to my activity and before my code reaches onActivityResult(). I suspect it has something to do with the way i'm crafting the intent, but i'm not sure. Here's my code:

alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "New", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent takePictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    });

If requested I can also add my onActivityResult code, though I'm not sure it's relevant here.

Here's the data from logcat:

12-02 10:47:43.976 26437-26437/? E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.google.android.apps.plus, PID: 26437 java.lang.IllegalArgumentException: A source URI must be specified via the Intent's data field. at dot.a(PG:185) at com.google.android.apps.photoeditor.fragments.PlusCropActivity.onCreate(PG:93) at android.app.Activity.performCreate(Activity.java:5933) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) 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:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Is this because of the way im launching the intent? Is there a way to keep this intent from trying to crop the photo, as im handling cropping in a separate intent?

Solution (I edited the code submitted by OferM):

try
{
      tempFile = File.createTempFile("my_app", ".jpg");

      File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
      File image = File.createTempFile("my_app", ".jpg",storageDir);
      uri = Uri.fromFile(image);
}catch(Exception e){}

alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "New", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
          Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          takePictureIntent.putExtras(getIntent().getExtras());
          takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
          startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
      }
});

Upvotes: 0

Views: 2676

Answers (1)

OferM
OferM

Reputation: 912

Try this:

alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "New", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent takePictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    });

where uri is the local path you want your image to be stored in. For example:

File tempFile = File.createTempFile("my_app", ".jpg");
fileName = tempFile.getAbsolutePath();
Uri uri = Uri.fromFile(tempFile);

Upvotes: 1

Related Questions