我正在尝试使用内置的相机应用程序,通过我自己的应用程序的意图调用它,在指定的地方拍照。我使用了来自developer.android.com的大量示例代码,但它引发了异常:
java.lang.RuntimException: Failure delivering result ResultInfro(who=null, request=1, result=1, data=null).
我检查SD卡但没有创建图像。我在这里读了很多帖子,但没有人帮我。
我的代码:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()) {
if (! mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
Uri fileUri = Uri.fromFile(getOutputMediaFile());
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, TAKE_PICTURE);
我接下来会做什么:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
//taken photo
case TAKE_PICTURE:
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
}
}
}
答案 0 :(得分:1)
这回答了一个非常相似的问题: I'm getting a NullPointerException when I use ACTION_IMAGE_CAPTURE to take a picture
最简单的解决方案是在启动活动时使用文件名保留实例变量,并在onActivityResult期间从该实例变量加载文件名。