android - 从单独的前台服务访问位于应用程序缓存目录中的文件时权限被拒绝
问题描述
我正在实现一个通过 AIDL 接口与远程前台服务通信的应用程序。在尝试从服务访问由应用程序在外部缓存文件夹中创建的文件时,我尝试通过 AIDL 将其作为意图打包的 URI 发送。我收到以下错误: Permission Denial: opening provider androidx.core.content.FileProvider from ProcessRecord{8a6853c 852:com.app.service/u0a89} (pid=852, uid=10089) 不是从 UID 10090 导出的
来自应用程序的代码:
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(),"com.app.fileprovider", myfile);
grantUriPermission("com.app",contentUri,Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
recognizer.sendFile(intent); // AIDL proxy call
服务代码
// AIDL sendFile stub body
Uri uri = intent.getData();
InputStream inputStream = null;
String str = "";
StringBuffer buf = new StringBuffer();
try {
inputStream = getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
Log.d(TAG, "FileNotFoundException");
}
我通过添加应用程序清单来根据 Android 文件提供程序指南调整清单:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.app.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
和 file_paths.xml 如下
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-cache-path name="my_files" path="." />
</paths>
通过 AIDL 接口共享 uri 意图的方法是否有效?我的前台服务没有任何活动的事实在这里起作用吗?此处的任何其他输入/指导都会有所帮助。
谢谢
解决方案
推荐阅读
- c# - OnTriggerEnter 调用两次
- node.js - 在 TypeScript 中使用 Promise 的 setTimeout() 和 clearTimeout()(严格模式 + 所有注释)
- javascript - NodeJS - 读取 Parquet 文件
- pytorch - 如何在 FloatTensor、Pytorch 中更改(分配)新值?
- assembly - 如何使用/链接 .lib 文件作为使用 TASM 汇编器的库?
- dropbox - 在 Dropbox 中看不到以前版本的菜单选项
- sql - 如何使用另一个表中的日期列过滤一个表的开始和结束日期
- swift - 用于 Apple 推送通知 AWS-SNS 或 Heroku 的更好的服务器是什么?
- html - 以 li 为中心,以 li::before 为中心
- c++ - 如何在 C++ 中打印到控制台?