当前位置: 首页 > 知识库问答 >
问题:

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

周云
2023-03-14

我试图保存图像到内部存储,我面临:

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/"/>

我的清单代码:

          <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>

这上面有什么线索吗?

共有1个答案

章侯林
2023-03-14

您的文件存储在私有目录cw中。getDir(“图像”,Context.MODE_PRIVATE) 选择文档中描述的其他可用解决方案

 类似资料: