使用相机捕获图像 - 无法提供结果ResultInfo

时间:2015-03-03 09:34:46

标签: android android-activity android-camera-intent

我试图用相机捕捉图像,我这样做:

启动相机:

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);
                                    }
                                });
                            }
                        });
            }

问题是当我回到应用程序时它的错误! 它的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

我调试我的应用程序,我认为这个字段:mLastPhoto为空!但我不知道为什么?当我启动相机时它有价值!

更新:

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();
        }
    }

2 个答案:

答案 0 :(得分:0)

尝试替换下面的getRealPathFromURI()方法,可能是因为弃用ManagedQuery(),请告诉我们您使用哪种设备有足够的空间?

public String getRealPathFromURI(Uri contentUri) {
    String res = null;
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
    if(cursor.moveToFirst()){;
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
       res = cursor.getString(column_index);
    }
    cursor.close();
    return res;
}

同时通过更新以下两行

来更新您的 onActivityResult
 if(resultCode == RESULT_OK) {
  if (requestCode == REQUEST_TAKE_PICTURE) 
      {
       // Put your code here/.....
   }
  }

答案 1 :(得分:0)

我以这种方式解决了我的问题(我不确定它是最好的方式,但对我有用):

 @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"));
    }