Android 11 无用户交互跨应用读写非媒体文件实现及FileProvider报错排查
规则背景
Android 11 强制实施了多项存储访问管控规则,对应用的外部存储读写范围做了严格限制。
实际业务场景
- 应用A需要向外部存储写入
.txt格式的纯文本文件 - 应用B需要在完全无用户交互的前提下,直接从外部存储读取该文件
- 在Android 11设备上执行读写操作时,直接抛出权限被拒绝的异常
前期调研结论
当前官方开放的跨应用外部存储访问能力,仅支持两类接口访问其他应用创建的文件:
- MediaStore API:仅覆盖图片、音频、视频类的媒体格式文件,不支持纯文本等非媒体类型
- 存储访问框架(Storage Access Framework):需要触发用户交互流程,由用户手动选择授权的文件夹路径,不符合无交互的使用要求
两类方案均无法适配当前的业务需求。
核心提问
在Android 11平台上,是否存在可行的替代方案,能够实现跨应用读取其他应用在外部存储创建的非媒体格式文件?
经过多轮调研,暂未找到匹配需求的落地路径。
问题更新:FileProvider跨应用共享的报错排查
尝试使用FileProvider实现跨应用文件共享时,启动目标Activity持续触发崩溃,完整异常栈如下:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.testapp, PID: 20141
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.app2.action.RECEIVE dat=content://com.example.testapp.fileprovider/myfiles/default_user.txt flg=0x1 }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2067)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1727)
at android.app.Activity.startActivityForResult(Activity.java:5320)
应用A(文件提供方)的启动代码
File filePath = new File(getFilesDir(), "files"); File newFile = new File(filePath, "default_user.txt"); Intent intent = new Intent(); intent.setAction("com.example.app2.action.RECEIVE"); intent.setData(FileProvider.getUriForFile(this, "com.example.testapp.fileprovider", newFile)); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(intent);
应用A的Manifest配置
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:requestLegacyExternalStorage="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.TestApp"> <activity android:name=".StorageActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity"> </activity> <service android:name=".service.TestService" android:enabled="true" android:exported="true" /> <provider android:name="androidx.core.content.FileProvider" android:authorities="com.example.testapp.fileprovider" android:grantUriPermissions="true" android:exported="false"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" /> </provider> </application>
应用B(文件接收方)的Manifest配置
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:requestLegacyExternalStorage="true" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.TestApp"> <activity android:name=".ReceiverActivity"> <intent-filter> <action android:name="com.example.app2.action.RECEIVE"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="content" android:host="com.example.testapp" /> </intent-filter> </activity> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
问题解答
无用户交互的跨应用非媒体文件访问可行路径
Android 11 严格的存储沙箱规则下,不存在通用的跨应用直接读写外部存储任意目录下非媒体文件的方案,可落地的替代路径有三类:
- 放弃外部存储公共目录,使用FileProvider完成文件共享
FileProvider是Android官方推荐的跨应用文件共享组件,不需要将文件写入外部存储公共目录,直接暴露应用私有目录下的指定文件给目标应用,全程无用户交互,天然绕开存储沙箱限制,是最匹配需求的方案。 - 同签名应用共享沙箱
如果两个应用使用同一签名文件打包,可通过配置android:sharedUserId让两个应用运行在同一Linux沙箱中,直接访问对方的私有目录文件,不需要额外申请存储权限。 - 系统级权限申请
仅针对厂商预装的系统应用,可申请MANAGE_EXTERNAL_STORAGE权限获取全量外部存储访问能力,普通第三方应用无法通过该权限的应用商店审核。
当前FileProvider启动崩溃的原因与修复方案
当前触发ActivityNotFoundException的核心原因是Intent的组件匹配规则不匹配,具体存在三个问题:
- Uri主机名配置不匹配
应用B的intent-filter中配置的data host是com.example.testapp,但应用A的FileProvider配置的authority是com.example.testapp.fileprovider,对应生成Uri的主机名是com.example.testapp.fileprovider,两者不匹配导致系统找不到可响应的Activity。
修复方式:将应用B的intent-filter中的host值修改为com.example.testapp.fileprovider,和FileProvider的authority保持一致。 - 缺少文件类型匹配规则
应用B的intent-filter仅配置了scheme和host,未补充txt文件对应的MIME类型,部分定制系统会因类型校验不通过无法完成组件匹配。可根据实际共享的文本文件类型,补充对应的data配置:<data android:scheme="content" android:host="com.example.testapp.fileprovider" android:mimeType="text/plain" /> - 缺少启动前校验
调用startActivity前可先通过resolveActivity方法校验系统中是否存在可响应当前Intent的组件,若不存在则提前做异常处理,避免应用崩溃。
内容的提问来源于stack exchange,提问作者Chong Choon Hong

