我是 Android 应用程序开发的新手。我正在尝试使用 Intents 拍照。一切都很好,我可以拍照并将其保存到外部存储中,但我无法找到它的保存位置。这是我的 onCreate 方法:
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
在这里我创建了保存图像的路径:
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
private static File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
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;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
图像保存在指定的路径中,但其中的数据onActivityResult
为空,我无法获取路径:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if( requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE )
{
if( resultCode == RESULT_OK ){
Toast.makeText(this, "Video saved to:\n" +data.getData(), Toast.LENGTH_LONG).show();
}
}
}