I have the code below to share a video file (.mp4) using an Android Sharesheet. The code works ok and the video does get shared but I have a few issues that do not meet my requirements.
I was hoping someone could help or point me in a direction to solve my issues.
- For this code to work the file must be downloaded first. This is not desirable because the user has to wait for the download and then the Sharesheet pops up. This causes two issues. a) User has to wait for DL to complete, b) If they cancel the download was unnecessary.
- QUESTION 1: How to I bring up the Sharesheet and then after the user selects the destination I download the file? This appears to be how TikTok works, select destination, progress during download, Complete Action Using Dialog appears.
- If the user pick Instagram for example, then a second Dialog appears that say "Complete action using"
- QUESTION 2: How can I customize the "Complete action using" Dialog?
I have looked at these articles and others:
https://developer.android.com/training/sharing/send https://developer.android.com/training/sharing/send#send-binary-content https://www.geeksforgeeks.org/how-to-share-image-from-url-with-intent-in-android/ https://androidlad.blogspot.com/2015/06/custom-sharing-intent-android-with.html https://gist.github.com/noln/584afabed1362fabbc2516275a5f755b
But none of those seem to show how to perform share functionality like TikTok and others.
My share code:
if (status == DownloadManager.STATUS_SUCCESSFUL) {
val fullPath: String?
val source: File?
val columnLocalURI = c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)
fullPath = c.getString(if (columnLocalURI > 0) columnLocalURI else 0)
source = File(Uri.parse(fullPath).path ?: "")
val share = source.toString()
val subject = context.getString(R.string.my_video)
val filename = share.substring(startIndex = share.lastIndexOf("/") + 1, endIndex = share.lastIndexOf("."))
MediaScannerConnection.scanFile(context, arrayOf(share), null) { _, uri ->
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "video/*"
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
shareIntent.putExtra(Intent.EXTRA_TITLE, filename)
shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
val pi = PendingIntent.getBroadcast(
context,
1001,
Intent(context, MyBroadcastReceiver::class.java),
PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
ContextCompat.startActivity(
context,
Intent.createChooser(shareIntent, "ShareWith", pi.intentSender),
null
)
showBusy.value = false
}
Here are some screenshot examples:
TikTok Select Destination (Instagram)
TikTok After file downloads this appears (Different depending on app, see other screen shot)
With another app when the app share button was tapped the file was downloaded and then then share sheet appeared. Tap Instagram destination
Then after Instagram was selected this appeared. Notice it is different than TikTok so it must be customized somehow.