First you need to have the data stored somewhere. If you are compiling against API 24 or above, the FileProvider is a popular choice:
Declare the provider in your AndroidManifest.xml
:
<application>
<!-- make sure within the application tag, otherwise app will crash with XmlResourceParser errors -->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.codepath.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/fileprovider" />
</provider>
</application>
Next, create a resource directory called xml
and create a fileprovider.xml
. Assuming you wish to grant access to the application's specific external storage directory, which requires requesting no additional permissions, you can declare this line as follows:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path
name="images"
path="Pictures" />
<!--Uncomment below to share the entire application specific directory -->
<!--<external-path name="all_dirs" path="."/>-->
</paths>
Finally, you will convert the File object into a content provider using the FileProvider class:
// getExternalFilesDir() + "/Pictures" should match the declaration in fileprovider.xml paths
val file = File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png")
// wrap File object into a content provider. NOTE: authority here should match authority in manifest declaration
val bmpUri = FileProvider.getUriForFile(MyActivity.this, "com.codepath.fileprovider", file)
Source
Now you have a way to store and retrieve Uri
for an individual File. The next step is to simply create an intent and start it by writing the following:
val intent = Intent().apply {
this.action = Intent.ACTION_SEND
this.putExtra(Intent.EXTRA_STREAM, bmpUri)
this.type = "image/jpeg"
}
startActivity(Intent.createChooser(intent, resources.getText(R.string.send_to)))
Notice the bmpUri
is the value you retrieved just before.
Source
You should remember to consider Runtime permissions if you are running API 23 or above. Here's a good tutorial on that.