6

Ours is a video hosting portal where users can upload and earn from their videos based on the views they get. We have recently launched an Android App and trying to integrate Share button to each video. Here is the code what we have placed

 Intent intent = new Intent();
                    try {

                        URL url = new URL("https://www.clipsnow.com/videos/images/thumbnails/230/10493.jpg");
                        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                        intent.setAction(Intent.ACTION_SEND);
                        intent.setData(Uri.parse("https://www.clipsnow.com"));

                        intent.putExtra(Intent.EXTRA_TEXT,msg);

                        intent.setType("text/plain");
                        intent.putExtra(Intent.EXTRA_STREAM, getImageUri(v.getContext(), image));


                        intent.setType("image/*");
                        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        v.getContext().startActivity(Intent.createChooser(intent, "Share Video"));

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

When we share any video with this, only thumbnail image is getting shared along with the video title. But, we need the video URL will get shared and when user tap on the URL, user will be taken to our app.

How can we do that?

aswarth
  • 61
  • 1
  • 1
  • 2

4 Answers4

4

This worked with me. Give a try!

File videoFile = new File(filePath);
Uri videoURI = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
        ? FileProvider.getUriForFile(mContext, mContext.getPackageName(), videoFile)
        : Uri.fromFile(videoFile);
ShareCompat.IntentBuilder.from(getActivity())
        .setStream(videoURI)
        .setType("video/mp4")
        .setChooserTitle("Share video...")
        .startChooser();
Hamzeh Soboh
  • 7,572
  • 5
  • 43
  • 54
3

You should download video first. Then you can share with using ACTION_SEND.

        String path = ""; //should be local path of downloaded video

        ContentValues content = new ContentValues(4);
        content.put(MediaStore.Video.VideoColumns.DATE_ADDED,
                System.currentTimeMillis() / 1000);
        content.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
        content.put(MediaStore.Video.Media.DATA, path);

        ContentResolver resolver = getApplicationContext().getContentResolver();
        Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);

        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("video/*");
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Hey this is the video subject");
        sharingIntent.putExtra(Intent.EXTRA_TEXT, "Hey this is the video text");
        sharingIntent.putExtra(Intent.EXTRA_STREAM,uri);
        startActivity(Intent.createChooser(sharingIntent,"Share Video");
iravul
  • 408
  • 9
  • 15
  • We don't want the user to download the video to local storage. If we share any video from YouTube via WhatsApp, it will share a thumbnail image, video title and video URL to the receiver. We want to the same kind of implementation to our app. – aswarth Apr 07 '17 at 04:48
  • 2
    @aswarth this is the different stuff. You should create post/video detail page and set html tags for it. Please check http://stackoverflow.com/a/35785393/1923925 also this http://stackoverflow.com/questions/19778620/provide-a-picture-for-whatsapp-link-sharing – iravul Apr 07 '17 at 12:33
  • @aswarth what Youtube share to whatsapp is just a link, and then Whatsapp fetch the Url link to get the image, and title, also descriptions if any. – Fthr Mar 26 '21 at 17:44
1

I guess, all the other solutions are obsolete. Here is the working solution for sharing the video to any platform (Youtube, Gmail, Hangout, Whatsapp etc),

startActivity(
    Intent.createChooser(
        Intent().setAction(Intent.ACTION_SEND)
            .setType("video/*")
            .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            .putExtra(
                Intent.EXTRA_STREAM,
                getVideoContentUri(this, File(currentVideo.videoPath))
            ), resources.getString(R.string.share_video)
    )
)

Below is the getVideoContentUri method,

/**
 * Return the URI for a file. This URI is used for
 * sharing of video.
 * NOTE: You cannot share a file by file path.
 *
 * @param context Context
 * @param videoFile File
 * @return Uri?
 */
fun getVideoContentUri(context: Context, videoFile: File): Uri? {
    var uri: Uri? = null
    val filePath = videoFile.absolutePath
    val cursor = context.contentResolver.query(
        MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
        arrayOf(MediaStore.Video.Media._ID),
        MediaStore.Video.Media.DATA + "=? ",
        arrayOf(filePath), null)

    if (cursor != null && cursor.moveToFirst()) {
        val id = cursor.getInt(cursor
            .getColumnIndex(MediaStore.MediaColumns._ID))
        val baseUri = Uri.parse("content://media/external/video/media")
        uri = Uri.withAppendedPath(baseUri, "" + id)
    } else if (videoFile.exists()) {
        val values = ContentValues()
        values.put(MediaStore.Video.Media.DATA, filePath)
        uri = context.contentResolver.insert(
            MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values)
    }

    closeCursor(cursor)
    return uri
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Abhishek Kumar
  • 4,532
  • 5
  • 31
  • 53
0

To send the video in android 10 and above use this. I used this to send the video from local storage to WhatsApp.

    public void shareVideo(String filepath)
    {
    
     Intent shareintent=new Intent("android.intent.action.SEND");      
     shareintent.setType("video/mp4");       
     shareintent.putExtra("android.intent.extra.STREAM", 
     Uri.parse(filepath));
     startActivity(Intent.createChooser(shareintent,"share"));

    }
halfer
  • 19,824
  • 17
  • 99
  • 186