我正在尝试使用内置的相机应用程序,通过我自己的应用程序的意图调用它,在指定的位置拍照。我使用了大量来自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
}
}
}