It seems that sharing data to other apps on Android M is only possible when the other app has manually asked for READ_EXTERNAL_STORAGE permission, and I am wondering if anyone knows a way around this without manually opening all apps I can share to and selecting Storage permission.
For example I have the following Intent to share 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..."));
Assume someFile
exists and that i have already followed the recommended Runtime Permission model suggested at http://developer.android.com/training/permissions/requesting.html and the user has agreed to allowing my Permission, so the File was in fact created on the system.
Is there a way to tell the Application I am sharing to, that I have granted this permission to my user can you please grant 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's data and any URIs specified in its ClipData.
And even more interesting that if this flag is set and i share to Gmail, the flag is not detected and Google does not prompt to request the permission but rather just catches the Exception and does not attach my file.
Any help would be appreciated.
You can share files by setting up a FileProvider
.
Define it in the AndroidManifest
like this:
<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 a xml file where you define the paths of the files that you want to share. For example, if you want to share a file which is in the external storage, the file_provider_paths.xml file will look like this:
<paths>
<external-path name="share_image" path="/MYAppFolder/Images/" />
</paths>
Then use your FileProvider
to get the content uri of the files that you want to share:
Uri uri = FileProvider.getUriForFile(context, "com.mydomain.fileprovider", file);
More information on the docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With