首页 > 解决方案 > 从单独的前台服务访问位于应用程序缓存目录中的文件时权限被拒绝

问题描述

我正在实现一个通过 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 意图的方法是否有效?我的前台服务没有任何活动的事实在这里起作用吗?此处的任何其他输入/指导都会有所帮助。

谢谢

标签: androidaidlandroid-fileprovider

解决方案


推荐阅读