Android M permissions for sharing data in other applications

It seems that data exchange with other applications on Android M is possible only when another application manually requested READ_EXTERNAL_STORAGE permission, and I'm wondering if anyone knows about this without manually opening all the applications for which I can share and choose Permission to storage.

For example, I have the following Intent for sharing some data:

Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("image/*"); shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Some Image Subject"); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(someFile)); startActivity(Intent.createChooser(shareIntent, "Share via...")); 

Suppose that someFile exists, and that I have already implemented the recommended runtime resolution model proposed in http://developer.android.com/training/permissions/requesting.html , and the user agreed to allow my permission, so the file is actually created in the system .

Is there a way to let me know that I am using an application to which I have granted this permission to my user, can you provide this on my behalf?

This does not work:

  shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

According to the documentation:

int FLAG_GRANT_READ_URI_PERMISSION

If set, the recipient of this intent will be granted permission to perform read operations on the URI in the Intent data and any URIs specified in its ClipData.

And even more interestingly, if this flag is set and I share with Gmail, the flag is not detected, and Google does not request permission, but simply catches the exception and does not attach my file.

Any help would be appreciated.

+5
source share
1 answer

You can share files by installing FileProvider .

Define it in AndroidManifest as follows:

 <manifest> ... <application> ... <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.mydomain.fileprovider" android:exported="false" android:grantUriPermissions="true"> ... <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_provider_paths" /> </provider> ... </application> </manifest> 

where authorities is an arbitrary name; then create an xml file in which you define the paths of the files you want to split. For example, if you want to share a file located in external storage, the file_provider_paths.xml file will look like this:

 <paths> <external-path name="share_image" path="/MYAppFolder/Images/" /> </paths> 

Then use FileProvider to get the contents of the uri of the files you want to provide:

 Uri uri = FileProvider.getUriForFile(context, "com.mydomain.fileprovider", file); 

More on docs

+2
source

Source: https://habr.com/ru/post/1233286/


All Articles