Ivan
Ivan

Reputation: 361

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

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

Upvotes: 0

Views: 4699

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007534

But why?

Because there is no persistable Uri permission in this scenario.

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.

Second, 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.

Third, SharedPreferences does not support multiple processes accessing its contents at the same time.

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.

Upvotes: 1

Related Questions