Android 从分离的前台服务访问位于应用程序缓存目录中的文件时,权限被拒绝

Android 从分离的前台服务访问位于应用程序缓存目录中的文件时,权限被拒绝,android,aidl,android-fileprovider,Android,Aidl,Android Fileprovider,我正在实现一个通过AIDL接口与远程前台服务通信的应用程序。 在尝试从服务访问由外部缓存文件夹中的应用程序创建的文件时,我尝试通过AIDL将其作为URI打包发送。我得到的错误如下: 权限拒绝:正在从ProcessRecord{8a6853c 852:com.app.service/u0a89}(pid=852,uid=10089)打开未从uid 10090导出的提供程序androidx.core.content.FileProvider 来自应用程序的代码: Uri contentUri

我正在实现一个通过AIDL接口与远程前台服务通信的应用程序。 在尝试从服务访问由外部缓存文件夹中的应用程序创建的文件时,我尝试通过AIDL将其作为URI打包发送。我得到的错误如下: 权限拒绝:正在从ProcessRecord{8a6853c 852:com.app.service/u0a89}(pid=852,uid=10089)打开未从uid 10090导出的提供程序androidx.core.content.FileProvider

来自应用程序的代码:

    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>

和文件_path.xml,如下所示

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-cache-path name="my_files" path="." />
</paths>

通过AIDL接口共享uri意图的方法有效吗? 我的前台服务没有任何活动这一事实能否在这里发挥作用? 此处的任何其他输入/指导都会有所帮助


谢谢

Uri contentUri=FileProvider.getUriForFile(getAppl…
。我们不知道该Uri会是什么样子,因为您没有告诉
contentUri.toString()
。还要告诉
myfile.getAbsoluthePath()
的值。还要告诉
Uri.toString()
的值=“com.app.fileprovider”和
“com.app”
不匹配。