gpt4 book ai didi

Android:FileProvider IllegalArgumentException 无法找到包含/data/data/的已配置根目录

转载 作者:搜寻专家 更新时间:2023-11-01 09:32:46 25 4
gpt4 key购买 nike

我正在尝试将图像保存到内部存储器,但我面临着:

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.example.example/app_images/f9732a4a-a22e-4b62-b9b3-e546c8edc93c.jpg.
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
at com.example.example.fragments.PhotosFragment.createImageFile(PhotosFragment.java:182)
at com.example.example.fragments.PhotosFragment.dispatchTakePictureIntent(PhotosFragment.java:191)

Java代码:

private File createImageFile() throws IOException {
ContextWrapper cw = new ContextWrapper(getActivity());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("images", Context.MODE_PRIVATE);
if(!directory.exists())
directory.mkdirs();
// Create imageDir
File mypath = new File(directory,UUID.randomUUID().toString() +".jpg");
if(!mypath.exists())
mypath.createNewFile();
Uri photoURI = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider",mypath);
return mypath;
}

private void dispatchTakePictureIntent() throws IOException {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
return;
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider",createImageFile());
mCurrentPhotoPath = "file:" + photoFile.getAbsolutePath();
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
}

我的路径代码:

  <files-path name="my_images" path="app_images/"/>

我的 list 代码:

          <provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.example.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>

有什么线索吗?

最佳答案

您将文件存储在私有(private)目录中 cw.getDir("images", Context.MODE_PRIVATE);选择 docs 中描述的另一个可用解决方案

关于Android:FileProvider IllegalArgumentException 无法找到包含/data/data/的已配置根目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45711705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com