我试图用相机拍照,但我得到以下错误:

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>

我一整天都在搜索这个错误,试图理解FileProvider,但我不知道这个错误消息试图告诉我什么。如果你想要更多信息/代码,请在评论中告诉我。


当前回答

Xamarin的。Android用户

这也可能是没有更新你的支持包时,针对Android 7.1, 8.0+。将它们更新到v25.4.0.2+,这个特定的错误可能就会消失(就像其他人所说的那样,您已经正确配置了file_path文件)。


背景介绍:我把目标从牛轧糖换成了奥利奥 Xamarin的。form应用和用Xam.Plugin.Media拍照 开始失败,出现上述错误消息,因此更新包 帮我做了这个戏法。

其他回答

这取决于你想做什么类型的存储,内部还是外部

用于外部存储

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

但是对于INTERNAL STORAGE,要小心路径,因为它使用getFilesDir()方法 这意味着你的文件将位于应用程序的根目录("/")

  File storeDir = getFilesDir(); // path "/"

所以你的提供者文件必须是这样的:

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

在启用口味(dev, stage)后出现类似的问题。

在口味之前,我的路径资源是这样的:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
    name="my_images"
    path="Android/data/pl.myapp/files/Pictures" />
</paths>

After added android:authorities="${applicationId}.fileprovider" 在Manifest中,appId是pl.myapp.dev或pl.myapp.stage,这取决于口味和应用程序开始崩溃。 我删除了full path,并将其替换为dot,一切都开始工作了。

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

如果你的文件路径是/storage/emulated/0/"yourfile"

你只需要修改你的FileProvider xml

<paths>
    <external-path name="external" path="." />
</paths>

当你需要共享文件时调用这个函数

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                    Uri fileUri = FileProvider.getUriForFile(getContext(),
                            "com.example.myapp.fileprovider",
                            file);
                    sharingIntent.setType("*/*"); // any flie type
                    sharingIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
                    startActivity(Intent.createChooser(sharingIntent, "Share file"));

它在android M ~ Pie上工作

我已经花了5个小时了。

我已经尝试了上面所有的方法,但这取决于你的应用程序当前使用的存储。

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

在尝试代码之前,请检查文档。

对我来说 因为文件路径子目录将是Context.getFilesDir()。 奇怪的是,Context.getFilesDir()注释了另一个子目录。

我要找的是

数据用户/ 0 / com . psh。mTest / app_imageDir / 20181202101432629 png。

Context.getFilesDir()

了 数据用户/ 0 / com . psh mTest / files。

标签应该是

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

那就有用了!!

花了两周时间试图找到一些解决方案…如果你在尝试了以上所有方法后到达这里:

1 -验证你的标签提供者是否在标签应用程序内

<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 -如果你尝试了很多方法都没有成功,那么就用这个方法来测试:

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

测试这个,如果相机工作,然后开始消除一些线,并继续测试…

3 -不忘记验证相机是否在模拟器中激活。