AndroidUtilCode icon indicating copy to clipboard operation
AndroidUtilCode copied to clipboard

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

Open Dreamxhx opened this issue 4 years ago • 2 comments

在调用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)

Dreamxhx avatar Sep 22 '21 07:09 Dreamxhx

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

sth0409 avatar May 25 '22 02:05 sth0409

@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)
        }
    }

sth0409 avatar May 25 '22 03:05 sth0409