Flutter安卓端截图分享报错:找不到配置根目录,添加权限无效
Flutter Android端截图分享报错:Failed to find configured root that contains...
问题现象
iOS端点击"Take Screenshot and Share"按钮可正常保存并分享截图,但Android端触发以下核心错误:
E/MethodChannel#plugins.flutter.io/share( 6505): java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.swize_frontend/cache/share/customerPhotos.png
已添加读写存储权限,但问题仍未解决。
问题原因
Android平台的Share插件依赖FileProvider实现跨应用文件共享,但你的App未配置FileProvider对应的文件路径规则,导致系统无法识别临时目录中的截图文件。这不是权限问题,而是FileProvider配置缺失。
解决步骤
1. 创建FileProvider路径配置文件
在Android项目的android/app/src/main/res/xml目录下(若无xml目录则手动新建)创建file_paths.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 配置应用内部临时目录 --> <cache-path name="cache_files" path="." /> <!-- 配置外部临时目录(可选,覆盖更多场景) --> <external-cache-path name="external_cache_files" path="." /> </paths>
2. 在AndroidManifest.xml注册FileProvider
在<application>标签内添加FileProvider注册代码,注意将android:authorities中的包名替换为你的App实际包名:
<provider android:name="androidx.core.content.FileProvider" android:authorities="com.example.swize_frontend.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
3. 重新编译运行
清理Android项目缓存后重新编译,点击分享按钮即可正常工作。
说明
getTemporaryDirectory()在Android上对应路径/storage/emulated/0/Android/data/[你的包名]/cache,通过<cache-path path="." />配置,FileProvider就能识别该目录下的所有文件,生成合法的共享Uri供Share插件调用。
内容的提问来源于stack exchange,提问作者Miangel
相关产品推荐
相关产品推荐

