I have a requirement to send a apk file through share Intent and I have also implemented without any hassle. But the problem arises only while sending apk via GMail, I am ge
When sharing files between applications, you should use a FileProvider as per the setting up file sharing training as this ensures that the receiving app can read the file without requiring any permissions. As of Android 6.0, Gmail does not request the storage permission, meaning it is unable to read any files you provide with Uri.fromFile()
.
You'd declare a FileProvider
in your manifest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.myapp.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
An XML file (in this case called filepaths.xml
to match the above manifest declaration) which determines which directories are available via the FileProvider
:
<paths>
<files-path path="images/" name="myimages" />
</paths>
Then, in place of using Uri.fromFile(file)
, you use FileProvider.getUriForFile(), passing in the same authority as in your manifest.