2

I'm trying to use external android camera to put an image inside an ImageView, but I get a "Failure delivering result ResultInfo" error. Here is my code:

Clicking the button to launch the camera app

public void takePhoto(View v) {


    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Creating the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            Log.e(TAG,"something went wrong", ex);
            return;
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_CODE);
        }
    }
}

Naming and creating a temp. file:

    String mCurrentPhotoPath;

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    String imageFileName = "JPEG" + timeStamp + "";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );
    if(image.exists() == false) {
        image.getParentFile().mkdirs();
        image.createNewFile();
    }


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

the onactivityresult:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// bla bla bla... I never get to this point.

Log.d(TAG,"request code: " + requestCode + "result code: " + resultCode);



}

My error:

06-03 14:35:50.121: E/AndroidRuntime(19125): FATAL EXCEPTION: main
06-03 14:35:50.121: E/AndroidRuntime(19125): Process: com.example.moimeme, PID: 19125
06-03 14:35:50.121: E/AndroidRuntime(19125): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.moimeme/com.example.moimeme.MainActivity}: java.lang.NullPointerException
06-03 14:35:50.121: E/AndroidRuntime(19125):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3365)
06-03 14:35:50.121: E/AndroidRuntime(19125):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
06-03 14:35:50.121: E/AndroidRuntime(19125):    at android.app.ActivityThread.access$1300(ActivityThread.java:135)
06-03 14:35:50.121: E/AndroidRuntime(19125):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
06-03 14:35:50.121: E/AndroidRuntime(19125):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-03 14:35:50.121: E/AndroidRuntime(19125):    at android.os.Looper.loop(Looper.java:136)
06-03 14:35:50.121: E/AndroidRuntime(19125):    at android.app.ActivityThread.main(ActivityThread.java:5017)
06-03 14:35:50.121: E/AndroidRuntime(19125):    at java.lang.reflect.Method.invokeNative(Native Method)
06-03 14:35:50.121: E/AndroidRuntime(19125):    at java.lang.reflect.Method.invoke(Method.java:515)
06-03 14:35:50.121: E/AndroidRuntime(19125):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-03 14:35:50.121: E/AndroidRuntime(19125):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-03 14:35:50.121: E/AndroidRuntime(19125):    at dalvik.system.NativeStart.main(Native Method)
06-03 14:35:50.121: E/AndroidRuntime(19125): Caused by: java.lang.NullPointerException
06-03 14:35:50.121: E/AndroidRuntime(19125):    at com.example.moimeme.MainActivity.onActivityResult(MainActivity.java:75)
06-03 14:35:50.121: E/AndroidRuntime(19125):    at android.app.Activity.dispatchActivityResult(Activity.java:5423)
06-03 14:35:50.121: E/AndroidRuntime(19125):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
06-03 14:35:50.121: E/AndroidRuntime(19125):    ... 11 more

I checked quite a few stack questions and changed my code accordingly but couldn't get it to work, sorry if missed anything. btw - debugged and tested on a nexus 5.

thanks in advance, and... I'm just starting out with the android platform, so excuse the nooby question/mistakes in the code :)

Jacob
  • 156
  • 1
  • 7

1 Answers1

6

Well, I found the solution eventually. Posting it here if someone needs it.

If using:

intent.putExtra(MediaStore.EXTRA_OUTPUT,
outputFileUri);

you are passing the extra "EXTRA_OUTPUT". When you pass this extra, you are already specifying where to store the captured image. Since you already know the location, the Intent passed in onActivityResult() will be null. i.e. data will be null.

That's why when you attempt to get the fetch Uri by

Uri uri = data.getData();

This will cause a NullPointerException. So, you just need to use the original path in the onActivityResult, instead of requesting it from getData.

Jacob
  • 156
  • 1
  • 7
  • Have the same issue. Your solution here do not work for me: even when I comment all references to `data` parameter from `onActivityresult(req, res, data)`, that error still happens. – 1111161171159459134 Aug 26 '15 at 04:37