從相機返回的android – onActivityResult,Intent為null
上的相機說明進行操作
我只是開始相機意圖,不建立自己的相機.
拍攝照片後處理結果返回的示例程式碼如下.
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可以,但是資料總是為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
你的程式碼的問題是這樣的:
data.getData()
此呼叫不會從返回的Intent中獲取關鍵字“data”的額外資訊.它從返回的Intent獲取欄位資料,該Intent可能為null.
你需要從這個返回的Intent中獲得額外的東西:
data.getExtras().get("data");
其他一些答案表明了這一點,嵌入了其他程式碼的噸數.這使得很難真正看到問題是什麼.
http://stackoverflow.com/questions/14558240/onactivityresult-returned-from-a-camera-intent-null