我只是启动相机意图,而不是构建自己的相机。
拍照后返回结果的处理示例代码如下。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Video captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Video saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the video capture
} else {
// Video capture failed, advise user
}
}
}
resultCode
可以,但data
始终为 NULL,这会导致 NPE。我查看了sdcard,照片真的保存在那里。任何提示?非常感谢。
更新:根据要求提供 logcat 信息:
01-28 19:39:00.547: ERROR/AndroidRuntime(24315): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {com.example.CameraTest/com.example.CameraTest.MyCamera}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.CameraTest/com.example.CameraTest.MyCamera}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2455)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2483)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1997)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3362)
at android.app.ActivityThread.access$700(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1162)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4511)
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:980)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.example.CameraTest/com.example.CameraTest.MyCamera}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:2991)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2442)
... 13 more
Caused by: java.lang.NullPointerException
at com.example.CameraTest.MyCamera.onActivityResult(MyCamera.java:71)
at android.app.Activity.dispatchActivityResult(Activity.java:4654)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2987)
... 14 more