Sharing bitmap in memory without asking for read p

2019-09-05 04:00发布

问题:

I am trying to store a bitmap within the app's storage using file provider and then share it via ACTION_SHARE, I am trying to avoid the read external storage permissions while sharing the image (it is the screenshot of the current view of the app -> bitmap).

Though I add the FLAG_GRANT_READ_URI_PERMISSION flag the receiver app always asks for the permissions, if I grant the permissions then the image is accessible by the receiver app else the receiver app does not show the image.

Can someone explain what the issue could be?

Should I create a custom ContentProvider instead of using FileProvider?

Here is my code:

Manifest:
<application>
...
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.user.samplescreenshotapp.fileprovider"
        android:grantUriPermissions="true"
        android:exported="false">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths" />
    </provider>
</application>


xml/filepaths:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="shared_images" path="images/"/>
</paths>

Code which triggers the share:

    File imagePath = new File(getCacheDir(), "images");
    File newFile = new File(imagePath, "image.png");
    Uri contentUri = FileProvider.getUriForFile(this, "com.example.user.samplescreenshotapp.fileprovider", newFile);
    Intent shareIntent = new Intent(Intent.ACTION_SEND, contentUri);

    shareIntent.setData(contentUri); //Tried with and without this

    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //Tried with setFlag also


    //shareIntent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    shareIntent.putExtra(Intent.EXTRA_TEXT, "sharing..");
    shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
    shareIntent.setType("image/png");

    startActivity(shareIntent); //Also tried startActivity(Intent.createChooser(shareIntent, "send"));