我试图使用外部Android摄像头将图像放入ImageView中,但是我得到了一个"失败的结果ResultInfo"错误。这是我的代码:
点击按钮启动相机应用
public void takePhoto(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Creating the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.e(TAG,"something went wrong", ex);
return;
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_CODE);
}
}
}
命名并创建临时文件。文件:
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String imageFileName = "JPEG" + timeStamp + "";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
if(image.exists() == false) {
image.getParentFile().mkdirs();
image.createNewFile();
}
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
Log.d(TAG,mCurrentPhotoPath);
return image;
}
onactivityresult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// bla bla bla... I never get to this point.
Log.d(TAG,"request code: " + requestCode + "result code: " + resultCode);
}
我的错误:
06-03 14:35:50.121: E/AndroidRuntime(19125): FATAL EXCEPTION: main
06-03 14:35:50.121: E/AndroidRuntime(19125): Process: com.example.moimeme, PID: 19125
06-03 14:35:50.121: E/AndroidRuntime(19125): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.moimeme/com.example.moimeme.MainActivity}: java.lang.NullPointerException
06-03 14:35:50.121: E/AndroidRuntime(19125): at android.app.ActivityThread.deliverResults(ActivityThread.java:3365)
06-03 14:35:50.121: E/AndroidRuntime(19125): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
06-03 14:35:50.121: E/AndroidRuntime(19125): at android.app.ActivityThread.access$1300(ActivityThread.java:135)
06-03 14:35:50.121: E/AndroidRuntime(19125): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
06-03 14:35:50.121: E/AndroidRuntime(19125): at android.os.Handler.dispatchMessage(Handler.java:102)
06-03 14:35:50.121: E/AndroidRuntime(19125): at android.os.Looper.loop(Looper.java:136)
06-03 14:35:50.121: E/AndroidRuntime(19125): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-03 14:35:50.121: E/AndroidRuntime(19125): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 14:35:50.121: E/AndroidRuntime(19125): at java.lang.reflect.Method.invoke(Method.java:515)
06-03 14:35:50.121: E/AndroidRuntime(19125): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-03 14:35:50.121: E/AndroidRuntime(19125): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-03 14:35:50.121: E/AndroidRuntime(19125): at dalvik.system.NativeStart.main(Native Method)
06-03 14:35:50.121: E/AndroidRuntime(19125): Caused by: java.lang.NullPointerException
06-03 14:35:50.121: E/AndroidRuntime(19125): at com.example.moimeme.MainActivity.onActivityResult(MainActivity.java:75)
06-03 14:35:50.121: E/AndroidRuntime(19125): at android.app.Activity.dispatchActivityResult(Activity.java:5423)
06-03 14:35:50.121: E/AndroidRuntime(19125): at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
06-03 14:35:50.121: E/AndroidRuntime(19125): ... 11 more
我检查了很多堆栈问题并相应地更改了我的代码,但是无法让它工作,如果遗漏任何事情,对不起。顺便说一下 - 在nexus 5上调试和测试。
提前感谢,并且......我刚刚开始使用android平台,请原谅代码中的小说问题/错误:)
答案 0 :(得分:7)
好吧,我最终找到了解决方案。如果有人需要它,请在此处发布。
如果使用:
intent.putExtra(MediaStore.EXTRA_OUTPUT,
outputFileUri);
你正在传递额外的“EXTRA_OUTPUT”。当您传递此额外内容时,您已经指定了存储捕获图像的位置。由于您已经知道了位置,因此传入onActivityResult()的Intent将为null。即数据将为空。
这就是为什么当你试图通过
获取获取Uri时的原因Uri uri = data.getData();
这将导致NullPointerException。因此,您只需要在onActivityResult中使用原始路径,而不是从getData请求它。