Search code examples
androidandroid-activityandroid-camera-intent

capture image with camera - Failure delivering result ResultInfo


Im trying to capture image with camera, i do it in this way :

start camera :

public void startPhotoTaker() {

            // create Intent to take a picture and return control to the calling application
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "cs_" + new Date().getTime() + ".jpg");
            mLastPhoto = Uri.fromFile(photo);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, mLastPhoto);

            // start the image capture Intent
            startActivityForResult(intent, REQUEST_TAKE_PICTURE);
        }

onActivityResult :

if (requestCode == REQUEST_TAKE_PICTURE) {
                /**
                 Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                 mediaScanIntent.setData(mLastPhoto);
                 this.sendBroadcast(mediaScanIntent);
                 */

                File file = new File(getRealPathFromURI(mLastPhoto));
                final Handler handler = new Handler();
                MediaScannerConnection.scanFile(
                        this, new String[]{file.toString()}, null,
                        new MediaScannerConnection.OnScanCompletedListener() {
                            public void onScanCompleted(String path, final Uri uri) {

                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        handleSendDelete(mLastPhoto, "image/*", true);
                                    }
                                });
                            }
                        });
            }

The problem is when i back to the app its colose with error ! its app logcat :

03-03 13:02:22.851: ERROR/AndroidRuntime(1808): FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to resume activity {info.guardianproject.otr.app.im/info.guardianproject.otr.app.im.app.ChatViewActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4, result=-1, data=null} to activity {info.guardianproject.otr.app.im/info.guardianproject.otr.app.im.app.ChatViewActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2444)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1986)
        at android.app.ActivityThread.access$600(ActivityThread.java:123)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4424)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4, result=-1, data=null} to activity {info.guardianproject.otr.app.im/info.guardianproject.otr.app.im.app.ChatViewActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2431)
        ... 12 more
        Caused by: java.lang.NullPointerException
        at info.guardianproject.otr.app.im.app.ChatViewActivity.getRealPathFromURI(ChatViewActivity.java:424)
        at info.guardianproject.otr.app.im.app.ChatViewActivity.onActivityResult(ChatViewActivity.java:336)
        at android.app.Activity.dispatchActivityResult(Activity.java:4649)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
        ... 13 more

I debug my app and i think this field : mLastPhoto is null ! but i dont know why ? it has value when i start the camera !

UPDATE :

public String getRealPathFromURI(Uri contentUri) {
        try {
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } catch (Exception e) {
            return contentUri.getPath();
        }
    }

Solution

  • I resolved my problem in this way (Im not sure its the best way but worked for me) :

     @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putString("mLastPhoto", String.valueOf(mLastPhoto));
        }
    
        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            mLastPhoto = Uri.parse(savedInstanceState.getString("mLastPhoto"));
        }