Search code examples
androidvideoandroid-sharing

Why does ACTION_SEND not behave like gallery's share button?


I have implemented the following code to share video:

// Copy video file to Fileprovider directory.
final String destFile = …

// Build FileProvider uri.
final Uri uri = FileProvider.getUriForFile(activity, AUTHORITY, destFile)

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.setType("video/*");
Log.d(TAG, "Sharing " + sendIntent.getType() + " for " + uri);

String title = "Share this content with";
Intent chooser = Intent.createChooser(sendIntent, title);

if (null != sendIntent.resolveActivity(activity.getPackageManager())) {
    activity.startActivity(chooser);
}

This code works and shows the chooser. When I select a messaging app like Telegram, I get to pick the destination conversation, but the video is sent as a file attachment, meaning that users only see a document icon and need to download it and open externally. However, the same video, using the OS system gallery, will open Telegram into their video editor where they can cut/edit the media and when sent will be visible inline in the conversation.

What am I missing to get the same behaviour? Looking through Android git repositories I don't see anything different from this to share content, so I don't know what I'm missing. The log I get with this code looks like

 Sharing video/* for content://com.app.android.fileprovider/share/video-a.mp4

So not only does it have the mime type but also the file extension could help. When I change the code to use a different mime type for images then I get the same behaviour, where the images get inlined into Telegram's chat.


Solution

  • For some reason the culprit was the FileProvider. Once I removed the FileProvider and passed in directly file:// URIs everything works.