0

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

LilMoke
  • 3,176
  • 7
  • 48
  • 88
  • "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, Sharesheet appears" -- in the first sentence, you say you want the share sheet *first*. In the second sentence, you say that TikTok does the share sheet *last*. Why are you citing TikTok as an example, if it does not do what you say that you want? – CommonsWare Nov 11 '22 at 14:40
  • "How can I customize the "Complete action using" Dialog?" -- you might want to upload a screenshot of what you are referring to. Device manufacturers often change system modals and sheets, and so particular messages that appear on your device might be different on others. – CommonsWare Nov 11 '22 at 14:41
  • I fixed my mistype from comment 1 and added screen shots as per comment 2. Those screenshots depict 2 different apps on the same phone both selecting Instagram as the sharesheet destination. The first downloads the file before the sharesheet appears, then second downloads after the share sheet selection, then once Instagram is selected they both show the "Complete Action" choices but they are different. – LilMoke Nov 11 '22 at 16:19

1 Answers1

0

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Yes, that may be true, I do suspect TikTok has custom screens there, but that is part of my question. #1 was how do I show the SHaresheet before the download of the file and the second was about customizing the screen – LilMoke Nov 11 '22 at 16:58
  • @LilMoke: "how do I show the SHaresheet before the download of the file" -- that is not an option. If you want to do what TikTok did, write your own bottom sheet. You can use `PackageManager` to either find if certain apps are installed or to query for who supports your specific `ACTION_SEND` `Intent`. "the second was about customizing the screen" -- you have no means to significantly change a platform-supplied chooser ("share sheet"). If you want to do what Instagram did, look into publishing sharing shortcuts. – CommonsWare Nov 11 '22 at 17:07
  • So to customize the Sharesheet there are articles on that, one here: https://matheustech.medium.com/custom-share-dialog-kotlin-8607a9c57c80 for example, so given that, I thought there may be a way to also customize the "Complete Action" dialog as well. As for downloading first, it seem strange that you cannot pick the destination first and then based on that decide what format/version/medium you will share to the chosen destination. – LilMoke Nov 11 '22 at 17:48
  • @LilMoke: "So to customize the Sharesheet there are articles on that" -- that shows how to write a share sheet-style bottom sheet from scratch. It is not about customizing a platform-supplied share sheet. – CommonsWare Nov 11 '22 at 18:08