Android sharesheet download file after destination selection

725 views Asked by At

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.

  1. 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.
  1. 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)

enter image description here

TikTok After file downloads this appears (Different depending on app, see other screen shot)

enter image description here

With another app when the app share button was tapped the file was downloaded and then then share sheet appeared. Tap Instagram destination

enter image description here

Then after Instagram was selected this appeared. Notice it is different than TikTok so it must be customized somehow.

enter image description here

1

There are 1 answers

4
CommonsWare On

Your first screenshot shows a bottom sheet created by and rendered by TikTok, written by TikTok developers. Almost everything in that sheet represents private actions within the TikTok service. If you wish to render your own bottom sheet and include buttons for some third-party apps in it, you can use queryIntentActivities() on PackageManager to find who can handle some Intent, such as ACTION_SEND. Note that the preferred method signature for queryIntentActivities() is a bit different on API Level 33. And note that on newer versions of Android, you will need a <queries> element in the manifest.

My guess is that your second screenshot is a chooser around ACTION_VIEW. You can confirm this by seeing what gets reported in Logcat when the sheet is shown and when the selected activity is started.

Your third screenshot shows what appears to be a typical Android platform "share sheet" for use with ACTION_SEND.

Your fourth screenshot shows the same "share sheet". I suspect that menu of actions comes from sharing shortcuts published by Instagram's developers.