【问题标题】:java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/java.lang.IllegalArgumentException:未能找到包含 /data/data/ 的已配置根目录
【发布时间】:2018-07-17 00:44:09
【问题描述】:

我正在尝试使用 ACTION_IMAGE_CAPTURE 意图捕获图像,然后将其保存到 /data/data/**/app_profile/profile_picture.jpg 我使用以下代码调用意图:

val cw = ContextWrapper(this@UserActivity.applicationContext)
val dir:File = cw.getDir("profile", Context.MODE_PRIVATE)

output = File(dir, "/profile_picture.jpg")
intent = Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)

**val newPhotoUri = FileProvider.getUriForFile(applicationContext, "$packageName.fileprovider", output)**

intent.putExtra(MediaStore.EXTRA_OUTPUT, newPhotoUri)
startActivityForResult(intent, REQUEST_CAMERA)

Log.e(TAG, "output: ${output.absolutePath}") ///data/user/0/**/app_profile/profile_picture.jpg

但我在FileProvider.getUriForFile() 上遇到以下崩溃

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/**/app_profile/profile_picture.jpg
    at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:418)
    at **.UserActivity.takePhoto(UserActivity.kt:147)

我在 Manifest 中有以下设置:

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

我的文件路径.xml:

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

我已经试过了

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

还有很多其他的组合。 有人可以指导我吗?

【问题讨论】:

  • 我自己修好了。而不是做 val dir:File = cw.getDir("profile", Context.MODE_PRIVATE)

标签: android kotlin android-fileprovider


【解决方案1】:

我自己修好了。而不是做

val dir:File = cw.getDir("profile", Context.MODE_PRIVATE)

我不得不这样做

val dir:File = cw.filesDir

返回正确的 /files/ 目录。 现在一切正常。

【讨论】:

    【解决方案2】:

    我查看了FileProvider的来源,发现了一个未记录的解决方案:

    &lt;root-path name="root_files" path="." /&gt;

    这显然会授予对您的应用可以访问的每个目录的访问权限。可能不是最安全的(尤其是如果您已经拥有WRITE_EXTERNAL_STORAGE),但它让您共享位于context.getDir() 下的文件,否则这是不可能的。

    如果可以的话,可能更好的解决方案是复制或移动context.getFilesDir() 下的文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多