Camera Intent返回Null

时间:2012-07-26 15:31:54

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

我正在尝试使用Intent使用默认摄像头从我的Android应用程序捕获图像和视频。

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

            startActivityForResult(intent, IMAGE_FROM_CAMERA);

使用上面的代码,我尝试捕获图像,onActivityResult我在data.getData()的数据中得到Null

我试图以意图传递Uri,但是Uri变空了。

相机意图无法在uri上写入图像。我需要uri通过电子邮件发送图像视频。

错误记录

 07-26 21:16:54.125: E/AndroidRuntime(6142): java.lang.RuntimeException: Failure    delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.cam/com.cam.CameraDemoActivity}: java.lang.NullPointerException
  07-26 21:16:54.125: E/AndroidRuntime(6142):   at android.app.ActivityThread.deliverResults(ActivityThread.java:3712)
  07-26 21:16:54.125: E/AndroidRuntime(6142):   at android.app.ActivityThread.handleSendResult(ActivityThread.java:3754)
07-26 21:16:54.125: E/AndroidRuntime(6142):     at android.app.ActivityThread.access$2800(ActivityThread.java:135)
07-26 21:16:54.125: E/AndroidRuntime(6142):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2162)
07-26 21:16:54.125: E/AndroidRuntime(6142):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 21:16:54.125: E/AndroidRuntime(6142):     at android.os.Looper.loop(Looper.java:143)
07-26 21:16:54.125: E/AndroidRuntime(6142):     at android.app.ActivityThread.main(ActivityThread.java:4914)
07-26 21:16:54.125: E/AndroidRuntime(6142):     at java.lang.reflect.Method.invokeNative(Native Method)
07-26 21:16:54.125: E/AndroidRuntime(6142):     at java.lang.reflect.Method.invoke(Method.java:521)
07-26 21:16:54.125: E/AndroidRuntime(6142):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-26 21:16:54.125: E/AndroidRuntime(6142):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
  07-26 21:16:54.125: E/AndroidRuntime(6142):   at dalvik.system.NativeStart.main(Native Method)
   07-26 21:16:54.125: E/AndroidRuntime(6142): Caused by: java.lang.NullPointerException
   07-26 21:16:54.125: E/AndroidRuntime(6142):  at com.cam.CameraDemoActivity.onActivityResult(CameraDemoActivity.java:48)
  07-26 21:16:54.125: E/AndroidRuntime(6142):   at android.app.Activity.dispatchActivityResult(Activity.java:3931)
   07-26 21:16:54.125: E/AndroidRuntime(6142):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3708)
   07-26 21:16:54.125: E/AndroidRuntime(6142):  ... 11 more

1 个答案:

答案 0 :(得分:1)

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
 File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
 File output = new File(dir, "camerascript.png");
 cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(output));
startActivityForResult(cameraIntent, 1);