我试图阅读并显示使用相机Intent拍摄的照片。 我的代码基于android docs中的示例:
public void takeSidePhoto(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile(REQUEST_IMAGE_CAPTURE_SIDE);
} catch (IOException ex) {
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this, "my.app.package.fileprovider", photoFile);
imageSide = photoURI.toString();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE_SIDE);
}
}
}
private File createImageFile(int imageCaptureCode) throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
return image;
}
问题是传递给photoURI
的{{1}}是(示例):
takePictureIntent
但是当我用文件管理器浏览我的测试设备存储时,我可以看到拍摄的图片存储在:
file:/storage/emulated/0/Android/data/my.app.package/files/Pictures/JPEG_20160921_123241_562024049.jpg
发生了什么以及如何获取真实文件的路径?
答案 0 :(得分:0)
在你的try块中做这样的事情来获取路径
if(photoFile.exists()){
String path = photoFile.getAbsolutePath()
}
答案 1 :(得分:0)
takeSidePhoto()方法应具有以下内容:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
imageUri = Uri.fromFile(photoFile);
} catch (IOException ex) {
Toast.makeText(getActivity(), ex.toString(), Toast.LENGTH_SHORT).show();
}
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
}
} else {
Toast.makeText(getActivity(), R.string.no_camera_error_message, Toast.LENGTH_SHORT).show();
}
在createImageFile()方法中,更改此行File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
使用此File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
答案 2 :(得分:0)
使用以下代码获取存储目录,并将图像保存到路径中。
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();