Blankj / AndroidUtilCode

:fire: Android developers should collect the following utils(updating).
https://blankj.com/2016/07/31/android-utils-code/
Apache License 2.0
33.24k stars 10.67k forks source link

在调用UriUtils.file2Uri(file)时候报的一个错误 #1544

Open Dreamxhx opened 3 years ago

Dreamxhx commented 3 years ago

在调用UriUtils.file2Uri(file)时候报的一个错误。线上统计的都是微信或者QQ目录下的错误

Thread Name: 'main' Back traces starts. java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/999/Pictures/WeiXin/mmexport1632153804305.jpg at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744) at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418) at com.blankj.utilcode.util.UriUtils.file2Uri(UriUtils.java:61)

sth0409 commented 2 years ago

并不是因为微信或者QQ目录,而是因为文件是应用分身创建的原因。我也正在找解决方案

sth0409 commented 2 years ago

@Dreamxhx 在fileprivider中添加

<root-path
        name="root-path"
        path="" />

创建自己的工具方法

fun file2Uri(file: File?): Uri? {
        if (!FileUtils.isFileExists(file)) return null
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            val authority = Utils.getApp().packageName + ".fileprovider"
            FileProvider.getUriForFile(
                Utils.getApp(), authority,
                file!!
            )
        } else {
            Uri.fromFile(file)
        }
    }