FileProvider.getUriForFile 不将图像保存到图库并且无法访问文件 OnActivityResult
FileProvider.getUriForFile doesn't save image to gallery and can't access file OnActivityResult
在我的应用中,尝试使用相机应用捕捉图像。
我正在更新我的应用程序以在指定用于保存图像的文件路径时使用 FileProvider.getUriForFile,因为 Uri.fromFile 已被弃用。
事先使用 Uri.fromFile 工作正常。但是我无法使用 FileProvider.getUriForFile.
让它工作
我正在创建图像文件如下:
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
然后,根据目标sdk,我得到图片URI:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
jobImageURI = Uri.fromFile(photoFile);
} else {
jobImageURI = FileProvider.getUriForFile(OrderDetails.this, BuildConfig.APPLICATION_ID + ".provider",photoFile);
}
我已经根据 android 开发人员文档更新了应用程序清单 xml 以包含提供程序。
我有一个包含路径的 provider_paths.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
然后我启动相机:
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, jobImageURI);
startActivityForResult(takePictureIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
然而,在拍摄照片并返回到 onActivityResult 函数后,我在图像文件中读回。
该图像未保存到图库中,因为我正在从图库中读回最后一个文件,但它不存在。
我也无法使用上面 jobImageURI 中的路径访问该文件。
是我提供的 provider_paths.xml 文件的路径不正确,还是我无法访问图片?
试试这个
在 res
目录的 xml
文件夹下创建一个 xml 文件
provider_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"
path="Android/data/yourAppPackageName/files/Pictures" />
</paths>
在 AndroidManifest.xml
中注册提供商
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="yourAppPackageName.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_path"/>
</provider>
add these permissions
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.CAMERA"/>
on Camera Button Click call dispatchTakePictureIntent()
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(getActivity(),
"com.arantico.servicepro.provider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
此处mCurrentPhotoPath
将显示存储文件的路径
在我的应用中,尝试使用相机应用捕捉图像。 我正在更新我的应用程序以在指定用于保存图像的文件路径时使用 FileProvider.getUriForFile,因为 Uri.fromFile 已被弃用。
事先使用 Uri.fromFile 工作正常。但是我无法使用 FileProvider.getUriForFile.
让它工作我正在创建图像文件如下:
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
然后,根据目标sdk,我得到图片URI:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
jobImageURI = Uri.fromFile(photoFile);
} else {
jobImageURI = FileProvider.getUriForFile(OrderDetails.this, BuildConfig.APPLICATION_ID + ".provider",photoFile);
}
我已经根据 android 开发人员文档更新了应用程序清单 xml 以包含提供程序。 我有一个包含路径的 provider_paths.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
然后我启动相机:
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, jobImageURI);
startActivityForResult(takePictureIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
然而,在拍摄照片并返回到 onActivityResult 函数后,我在图像文件中读回。 该图像未保存到图库中,因为我正在从图库中读回最后一个文件,但它不存在。 我也无法使用上面 jobImageURI 中的路径访问该文件。
是我提供的 provider_paths.xml 文件的路径不正确,还是我无法访问图片?
试试这个
在 res
目录的 xml
文件夹下创建一个 xml 文件
provider_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"
path="Android/data/yourAppPackageName/files/Pictures" />
</paths>
在 AndroidManifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="yourAppPackageName.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_path"/>
</provider>
add these permissions
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.CAMERA"/>
on Camera Button Click call
dispatchTakePictureIntent()
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(getActivity(),
"com.arantico.servicepro.provider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
此处mCurrentPhotoPath
将显示存储文件的路径