Android文件权限报错

这个错误 Couldn't find meta-data for provider with authority com.airdial.fileprovider 非常明确,意思是:你在代码中尝试使用 FileProvider,但在 AndroidManifest.xml 中没有找到对应的配置,或者配置的 authority 名字不匹配。

要解决这个问题,你需要完成以下 两步 配置:

第一步:修改 AndroidManifest.xml

你需要并在 <application> 标签内添加 <provider> 声明。

请确保 android:authorities 的值与你代码中生成的字符串一致。你代码中是 ${packageName}.fileprovider,也就是 com.airdial.fileprovider

打开 src/main/AndroidManifest.xml,添加以下代码:

xml 复制代码
<manifest ...>
    <application ...>

        <!-- 添加这段 Provider 配置 -->
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

        <!-- 其他 activity, service 等 ... -->

    </application>
</manifest>

注意

  1. android:authorities="${applicationId}.fileprovider":这里使用 ${applicationId} 是为了让 Gradle 自动替换为你的包名(com.airdial)。如果你的 build.gradleapplicationId 就是 com.airdial,那么它解析出来就是 com.airdial.fileprovider,这与你代码中的逻辑匹配。
  2. android:resource="@xml/file_paths":这引用了下一步要创建的资源文件。

第二步:创建 res/xml/file_paths.xml

你的代码中使用了 Environment.getExternalStorageDirectory(),这对应的是外部存储根目录。你需要告诉 FileProvider 允许共享这个目录下的文件。

  1. res 目录下新建一个文件夹叫 xml(如果不存在)。
  2. res/xml 下新建文件 file_paths.xml
  3. 填入以下内容:
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <!-- 
      external-path 对应 Environment.getExternalStorageDirectory()
      name 是 URI 中显示的虚拟目录名
      path="." 表示允许访问根目录下的所有文件
    -->
    <external-path name="external_files" path="." />
    
    <!-- 如果你也需要访问相册/图片目录,可以加上这个 -->
    <external-files-path name="my_images" path="Pictures" />
    
    <!-- 对应 Context.getFilesDir() -->
    <files-path name="files" path="." />
    
    <!-- 对应 Context.getCacheDir() -->
    <cache-path name="cache" path="." />
</paths>

针对你代码的具体解释: 你的代码中文件路径是: File(Environment.getExternalStorageDirectory(), "Documents/1.pdf") 这需要 <external-path> 才能被 FileProvider 解析。


第三步:检查代码中的 Authority (可选)

你的代码是这样写的:

kotlin 复制代码
FileProvider.getUriForFile(getCurActivity(), "${getCurActivity().baseContext.packageName}.fileprovider", file)

只要你的 AndroidManifest.xml 里的 package 属性或者 build.gradle 里的 applicationIdcom.airdial,那么这段代码生成的字符串就是 com.airdial.fileprovider

建议微调 : 为了更稳健(防止 Context 包装器导致获取包名异常),建议直接使用 context.packageNameBuildConfig.APPLICATION_ID(如果有生成的话):

kotlin 复制代码
private fun getUriForFile(file: File): Uri? {
    return try {
        // 使用 applicationContext 或者直接使用 activity 的 packageName 属性通常更安全
        val authority = "${getCurActivity().packageName}.fileprovider"
        FileProvider.getUriForFile(getCurActivity(), authority, file)
    } catch (e: Exception) {
        e.printStackTrace()
        simLog("> Error:getUriForFile: ${e.localizedMessage}")
        null
    }
}

总结

你报错的核心原因仅仅是 AndroidManifest.xml 缺少了 <provider> 标签 或者 缺少了 @xml/file_paths 文件。按照上述两步补全即可解决。

相关推荐
Jomurphys2 小时前
Compose 适配 - 全屏显示 EdgeToEdge
android
ii_best2 小时前
「安卓开发辅助工具按键精灵」xml全分辨率插件jsd插件脚本教程
android·xml·开发语言·编辑器·安卓
消失的旧时光-19432 小时前
从 Android 回调到 C 接口:函数指针 + void* self 的一次彻底理解
android·c语言·开发语言
dvlinker2 小时前
动态代理技术实战测评—高效解锁Zillow房价历史
android·java·数据库
峥嵘life2 小时前
Android16 EDLA 认证BTS测试Failed解决总结
android·java·linux·运维·学习
二流小码农2 小时前
鸿蒙开发:自定义一个圆形动画菜单
android·ios·harmonyos
wniuniu_2 小时前
object->osd
android·java·数据库
封妖九禁苍天泣3 小时前
Android WebView 使用本地字体-WebViewAssetLoader
android
00后程序员张3 小时前
fastlane 结合 appuploader 命令行实现跨平台上传发布 iOS App
android·ios·小程序·https·uni-app·iphone·webview