Android FileProvider报错:Failed to find provider info 求助
问题排查与解决步骤
1. 核对AndroidManifest中FileProvider的完整配置
必须保证<provider>标签的核心属性和meta-data配置无遗漏:
- 强制设置
android:exported="false"(FileProvider要求禁止导出) - 强制设置
android:grantUriPermissions="true"(允许临时授权邮件应用访问文件) - meta-data的
android:resource必须指向正确的xml路径,若文件在res/xml目录下,需写为@xml/provider_paths
正确配置示例:
<provider android:name="androidx.core.content.FileProvider" android:authorities="com.test.example.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /> </provider>
2. 验证provider_paths.xml的files-path节点匹配性
你的文件存在context.filesDir下,对应files-path节点,需确保节点的name属性与生成Uri时的路径段完全一致。比如你生成的Uri是content://com.test.example.fileprovider/files_path/logcat.log,xml中必须配置:
<files-path name="files_path" path="." />
path="."表示映射整个filesDir目录,name值将作为Uri中的路径段,必须和代码逻辑对应。
3. 检查Uri生成代码的正确性
调用FileProvider.getUriForFile时需注意三个参数的准确性:
- 第一个参数必须传ApplicationContext(避免内存泄漏,同时保证全局可用性)
- 第二个参数必须与Manifest中配置的
authorities完全一致(即com.test.example.fileprovider) - 第三个参数是
logcat.log对应的File对象
示例代码:
File logFile = new File(context.getFilesDir(), "logcat.log"); Uri contentUri = FileProvider.getUriForFile(context.getApplicationContext(), "com.test.example.fileprovider", logFile);
4. 修正邮件Intent的Action类型
Intent.ACTION_SENDTO仅支持纯文本邮件(基于mailto:协议),无法附加文件。需改用Intent.ACTION_SEND,并配置正确的MIME类型与临时权限:
示例代码:
Intent emailIntent = new Intent(Intent.ACTION_SEND); emailIntent.setType("text/plain"); // 若为二进制文件可设为"application/octet-stream" emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"收件人邮箱地址"}); emailIntent.putExtra(Intent.EXTRA_SUBJECT, "日志文件"); emailIntent.putExtra(Intent.EXTRA_STREAM, contentUri); // 授予邮件应用临时读取权限 emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // 启动应用选择器 startActivity(Intent.createChooser(emailIntent, "选择邮件应用发送日志"));
5. 确认provider_paths.xml的位置与命名
确保文件放在res/xml目录下,文件名完全匹配(Android资源文件名区分大小写),若res/xml目录不存在需手动创建。
内容的提问来源于stack exchange,提问作者Nikitc
相关产品推荐
相关产品推荐

