FileProvider - IllegalArgumentException: Failed to

2020-01-23 15:19发布

I'm trying to take a picture with camera, but I'm getting the following error:

FATAL EXCEPTION: main
Process: com.example.marek.myapplication, PID: 6747
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.marek.myapplication/files/Pictures/JPEG_20170228_175633_470124220.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.marek.myapplication.MainActivity.dispatchTakePictureIntent(MainActivity.java:56)
    at com.example.marek.myapplication.MainActivity.access$100(MainActivity.java:22)
    at com.example.marek.myapplication.MainActivity$1.onClick(MainActivity.java:35)

AndroidManifest.xml:

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.marek.myapplication.fileprovider"
        android:enabled="true"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
</provider>

Java:

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            Toast.makeText(getApplicationContext(), "Error while saving picture.", Toast.LENGTH_LONG).show();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.marek.myapplication.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }

file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path name="my_images" path="images/"/>
</paths>

I was searching whole day about this error and trying to understand FileProvider, but I have no idea what this error message tries to tell me. If you want more info/code, write me in the comment.

30条回答
Lonely孤独者°
2楼-- · 2020-01-23 15:44

If nothing helps and you are getting the error

failed to find configured root that contains /data/data/...

then try changing some line like:

File directory = thisActivity.getDir("images", Context.MODE_PRIVATE);

to:

File directory = new File(thisActivity.getFilesDir(), "images");

and in the xml file:

<files-path name="files" path="." />

which is weird, since the folder I access is /images.

查看更多
三岁会撩人
3楼-- · 2020-01-23 15:45

The following change in xml file_paths file worked for me.

 <external-files-path name="my_images" path="Pictures"/>
 <external-files-path name="my_movies" path="Movies"/>
查看更多
手持菜刀,她持情操
4楼-- · 2020-01-23 15:46

I see that at least you're not providing the same path as others in file_paths.xml. So please make sure you provide the exact the same package name or path in 3 places including:

  • android:authorities attribute in manifest
  • path attribute in file_paths.xml
  • authority argument when calling FileProvider.getUriForFile().
查看更多
来,给爷笑一个
5楼-- · 2020-01-23 15:48

Lost 2 weeks trying to find some solution... If you arrive here after try everthing above:

1 - Verify if your tag provider are inside tag application

<application>

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

</application>

2 - If you try a lot paths way without success, then test with this:

<?xml version="1.0" encoding="utf-8"?>
<paths>
  <cache-path name="cache" path="." />
  <external-path name="external" path="." />

  <root-path name="root" path="." />
  <files-path name="my_images" path="/" />
  <files-path name="my_images" path="myfile/"/>
  <files-path name="files" path="." />

  <external-path name="external_files" path="." />
  <external-path name="images" path="Pictures" />
  <external-path name="my_images" path="." />
  <external-path name="my_images" path="Android/data/com.companyname.yourproject/files/Pictures" />
  <external-path name="my_images" path="Android/data/com.companyname.yourproject/files/Pictures/" />

  <external-files-path name="images" path="Pictures"/>
  <external-files-path name="camera_image" path="Pictures/"/>
  <external-files-path name="external_files" path="." />
  <external-files-path name="my_images" path="my_images" />

  <external-cache-path name="external_cache" path="." />

</paths>

Test this, if camera works, then start eliminate some lines and continue testing...

3 - No forget verify if camera are actived in your emulator.

查看更多
Ridiculous、
6楼-- · 2020-01-23 15:49

I have spent 5 hours for this..

I have tried all the methods above but it depends on the what storeage your app currently using.

https://developer.android.com/reference/android/support/v4/content/FileProvider#GetUri

Check the documentation before trying the codes.

In my case since files-path sub directory will be Context.getFilesDir(). The funky thing is it Context.getFilesDir() notes one another subdirectory.

what I am looking for is

data/user/0/com.psh.mTest/app_imageDir/20181202101432629.png

Context.getFilesDir()

returns /data/user/0/com.psh.mTest/files

so the tag should be

....files-path name="app_imageDir" path="../app_imageDir/" ......

Then it works!!

查看更多
Emotional °昔
7楼-- · 2020-01-23 15:51

Your file is stored under getExternalFilesDir(). That maps to <external-files-path>, not <files-path>. Also, your file path does not contain images/ in it, so the path attribute in your XML is invalid.

Replace res/xml/file_paths.xml with:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path name="my_images" path="/" />
</paths>
查看更多
登录 后发表回答