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

时间:2017-08-16 10:57:27

标签: android android-fileprovider

我试图将图像保存到内部存储空间而我正面临着:

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 个答案:

答案 0 :(得分:2)

您将文件存储在私人目录cw.getDir("images", Context.MODE_PRIVATE);中 选择docs

中描述的另一种可用溶剂