【发布时间】:2016-09-21 10:23:49
【问题描述】:
我正在尝试读取并显示使用相机 Intent 拍摄的照片。 我的代码基于 android 文档中的示例:
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;
}
问题在于传递给takePictureIntent 的photoURI 是(示例):
file:/storage/emulated/0/Android/data/my.app.package/files/Pictures/JPEG_20160921_123241_562024049.jpg
但是当我使用文件管理器浏览我的测试设备存储时,我可以看到拍摄的图片存储在:
file:/storage/Android/data/my.app.package/files/Pictures/JPEG_20160921_123241_562024049.jpg
发生了什么?如何获取真实文件的路径?
【问题讨论】:
-
FileProvider不会使用来自getUriForFile()的file方案返回Uri值。它使用content方案返回Uri值。除此之外,请准确说明您使用的是什么“文件管理器”。
标签: android file android-intent camera