Capturing from Camera resulting in Failure delivering result ResultInfo

586 views Asked by At

I am getting the following error:

Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity

After done taking picture with my camera.

This is the code for onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(resultCode == RESULT_OK)
    {
        if(requestCode == 1888)
        {
            final boolean isCamera;
            if(data == null)
            {
                isCamera = true;
            }
            else
            {
                final String action = data.getAction();
                if(action == null)
                {
                    isCamera = false;
                }
                else
                {
                    isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                }
            }

            Uri selectedImageUri;
            if(isCamera)
            {
                selectedImageUri = outputFileUri;
            }
            else
            {
                selectedImageUri = data == null ? null : data.getData();
            }

            Log.i("DEBUG", "IMAGEURI: " + selectedImageUri);
            coverPhoto.setImageURI(selectedImageUri);
            setRealPath(selectedImageUri); <--- if i remove this it works.
        }
    }
}

This is the code i use to grab the real path.

public String getRealPathFromURI(Context context, Uri contentUri) {
  Cursor cursor = null;
  try { 
    String[] proj = {MediaStore.Images.Media.DATA };
    cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);

    // ERROR ORIGINATING THE LINE BELOW
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
  } finally {
    if (cursor != null) {
      cursor.close();
    }
  }
}

Some getters and setters

public void setRealPath(Uri uri){
    this.realPath = getRealPathFromURI(getApplicationContext(),uri);
}

public String getRealPath(){
    return this.realPath;
}

I test this using genymotion emulator. And i have set my shared folder from VirtualBox.

Shared folder

However, i couldn't see any picture from My Picture (as set from VirtualBox).

Full LogCat:

01-09 09:16:42.293: E/AndroidRuntime(1921): FATAL EXCEPTION: main
01-09 09:16:42.293: E/AndroidRuntime(1921): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {com.gelliesmedia.thumbqoo/com.gelliesmedia.thumbqoo.ProductPublishActivity}: java.lang.NullPointerException
01-09 09:16:42.293: E/AndroidRuntime(1921):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
01-09 09:16:42.293: E/AndroidRuntime(1921):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
01-09 09:16:42.293: E/AndroidRuntime(1921):     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
01-09 09:16:42.293: E/AndroidRuntime(1921):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
01-09 09:16:42.293: E/AndroidRuntime(1921):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 09:16:42.293: E/AndroidRuntime(1921):     at android.os.Looper.loop(Looper.java:137)
01-09 09:16:42.293: E/AndroidRuntime(1921):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-09 09:16:42.293: E/AndroidRuntime(1921):     at java.lang.reflect.Method.invokeNative(Native Method)
01-09 09:16:42.293: E/AndroidRuntime(1921):     at java.lang.reflect.Method.invoke(Method.java:511)
01-09 09:16:42.293: E/AndroidRuntime(1921):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-09 09:16:42.293: E/AndroidRuntime(1921):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-09 09:16:42.293: E/AndroidRuntime(1921):     at dalvik.system.NativeStart.main(Native Method)
01-09 09:16:42.293: E/AndroidRuntime(1921): Caused by: java.lang.NullPointerException
01-09 09:16:42.293: E/AndroidRuntime(1921):     at com.gelliesmedia.thumbqoo.ProductPublishActivity.getRealPathFromURI(ProductPublishActivity.java:267)
01-09 09:16:42.293: E/AndroidRuntime(1921):     at com.gelliesmedia.thumbqoo.ProductPublishActivity.setRealPath(ProductPublishActivity.java:357)
01-09 09:16:42.293: E/AndroidRuntime(1921):     at com.gelliesmedia.thumbqoo.ProductPublishActivity.onActivityResult(ProductPublishActivity.java:351)
01-09 09:16:42.293: E/AndroidRuntime(1921):     at android.app.Activity.dispatchActivityResult(Activity.java:5293)
01-09 09:16:42.293: E/AndroidRuntime(1921):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
01-09 09:16:42.293: E/AndroidRuntime(1921):     ... 11 more

Can anyone tell me how to solve this error? thanks.

1

There are 1 answers

0
karl On

I am using similar code to detect whether a resulting image is from the camera or not. It appears that on certain devices (mainly Motorola and HTC), data.getAction() will be null even for a result Intent coming from a camera. This means that selectedImageUri becomes null. When you pass that through into the query context.getContentResolver().query(contentUri, proj, null, null, null);, the resulting Cursor is null, so you get a NPE when you try to interact with it.

I'm working on a different way to determine if the image is from the camera and will update when I find a solution.

UPDATE 1/24: I haven't been able to come up with a better way to determine whether or not the image is from the camera. It looks like some devices simply report the Uri differently and there's not a good way to check that. However, it does appear to be safe to use the raw Uri if the cursor is null:

    /** snip **/
    cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
    if (cursor == null) {
        return contentUri;
    }
    /** carry on **/

I've tested this on a couple different phones, and it appears to work with camera images, gallery images, and images from other sources, such as Dropbox.