I searched and read android developer files but I did not find any satisfactory answer for content provider grant uri permission. Can anybody explain more detailed and more simple. My questions are: What grant uri is using for? What is the differences between grant uri permission true and false When we should use true? when false? and any more detail are appreciated.
问题:
回答1:
What grant uri is using for?
The "grant Uri
permissions" feature allows you to have a ContentProvider
that is normally inaccessible by third parties, yet selectively allow access to individual Uri
values to individual third-party apps for a short period of time (e.g., long enough to view the PDF that the provider serves).
What is the differences between grant uri permission true and false
android:grantUriPermissions="true"
indicates that your Java code can use FLAG_GRANT_READ_URI_PERMISSION
and FLAG_GRANT_WRITE_URI_PERMISSION
for any Uri
served by that ContentProvider
.
android:grantUriPermissions="false"
indicates that only the Uri
values specified by child <grant-uri-permission>
elements can be used with FLAG_GRANT_READ_URI_PERMISSION
and FLAG_GRANT_WRITE_URI_PERMISSION
.
回答2:
Say you need to mail some file that is on your application cache directory.
No other apps can access that file, unless you specify that other apps can access content of your application. For this you create content provider, and say, all uri's in form content://com.your.app/file you 'redirect' to your application cache directory.
Some code:
File f = ...; // Some local file.
Uri uri = Uri.parse("content://com.your.app/" + f.getName());
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Body");
intent.putExtra(Intent.EXTRA_STREAM, uri);
// You only can add flag FLAG_GRANT_READ_URI_PERMISSION if your app has
// android:grantUriPermissions="true" in manifest or see quote below.
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(intent, "Send Email"));
As CommonsWare said:
android:grantUriPermissions="false" indicates that only the Uri values specified by child <grant-uri-permission> elements can be used with FLAG_GRANT_READ_URI_PERMISSION and FLAG_GRANT_WRITE_URI_PERMISSION.