简体   繁体   English

Android - 未授予 URI 权限

[英]Android - URI permission is not granted

I'm developing two apps that will share some files.我正在开发两个将共享一些文件的应用程序。 When I run the following intent当我运行以下意图时

    Intent next = new Intent(Intent.ACTION_VIEW,
            Uri.fromFile(new File("/data/data/com.myapp1/shared_prefs/CookiePreferences.xml")));
    next.setClassName("com.myapp2", "com.myapp2.SomeActivity");
    next.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(next);

And in the second recipient app this code:在第二个收件人应用程序中,此代码:

        Uri uri = intent.getData();
        grantUriPermission(getPackageName(), uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        InputStream s = getContentResolver().openInputStream(uri);

I get the following exception:我收到以下异常:

java.lang.SecurityException: No permission grant found for UID 10242 and Uri file:///data/data/com.myapp1/shared_prefs/CookiePreferences.xml java.lang.SecurityException:未找到 UID 10242 和 Uri file:///data/data/com.myapp1/shared_prefs/CookiePreferences.xml 的权限授予

But why?但为什么? I need to grant this permission exactly via sending intent.我需要通过发送意图完全授予此权限。 What I'm doing wrong?我做错了什么? I put a lot of calls like takePersistableUriPermission and grantUriPermission using advises from the internet, but these calls didn't help我使用来自互联网的建议进行了很多调用,例如takePersistableUriPermissiongrantUriPermission ,但这些调用没有帮助

But why?但为什么?

Because there is no persistable Uri permission in this scenario.因为在这种情况下没有持久化的Uri权限。

What I'm doing wrong?我做错了什么?

First, in your second code snippet, get rid of the grantUriPermission() line and the takePersistableUriPermission() line, as neither are necessary or will work.首先,在您的第二个代码片段中,去掉grantUriPermission()行和takePersistableUriPermission()行,因为它们都不是必需的,也不会起作用。

Second, Uri.fromFile(new File("/data/data/com.myapp1/shared_prefs/CookiePreferences.xml")));二、 Uri.fromFile(new File("/data/data/com.myapp1/shared_prefs/CookiePreferences.xml"))); will not work, as the second app does not have access to this file, and FLAG_GRANT_READ_URI_PERMISSION is only for content Uri values.将不起作用,因为第二个应用程序无权访问此文件,并且FLAG_GRANT_READ_URI_PERMISSION仅适用于content Uri值。

Third, SharedPreferences does not support multiple processes accessing its contents at the same time.第三, SharedPreferences不支持多个进程同时访问其内容。

Instead, attach extras to the Intent with the data that the second activity needs, and stop trying to use the same SharedPreferences in two separate apps.相反,使用第二个 Activity 需要的数据将附加内容附加到Intent ,并停止尝试在两个单独的应用程序中使用相同的SharedPreferences

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM