Android : Share intent is not working for video file path
Asked Answered
S

3

9

I have a video file path and want to share the video on social media, but unable to share the video. I am trying following code in Android Studio 2.2, but it's not working.

Code snippet :

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button shareBtn = (Button) findViewById(R.id.sharebutton);

    shareBtn .setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v) {

                    File f = new File("/sdcard/VID_20161201123613.mp4");
                    Uri uriPath = Uri.parse(f.getPath());

                    Intent shareIntent = new Intent();
                    shareIntent.setAction(Intent.ACTION_SEND);
                    shareIntent.putExtra(Intent.EXTRA_TEXT, "Text");                  
                    shareIntent.putExtra(Intent.EXTRA_STREAM, uriPath);
                    shareIntent.setType("video/*");
                    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    startActivity(Intent.createChooser(shareIntent, "send"));

                }
            }
    );
}
Surname answered 1/12, 2016 at 7:30 Comment(2)
U want to show list to shareIvonneivor
any solution yet?Matheson
I
5

Try This :

public void shareVideo(final String title, String path) {

MediaScannerConnection.scanFile(getActivity(), new String[] { path },

            null, new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                    Intent shareIntent = new Intent(
                            android.content.Intent.ACTION_SEND);
                    shareIntent.setType("video/*");
                    shareIntent.putExtra(
                            android.content.Intent.EXTRA_SUBJECT, title);
                    shareIntent.putExtra(
                            android.content.Intent.EXTRA_TITLE, title);
                    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                    shareIntent
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                    context.startActivity(Intent.createChooser(shareIntent,
                            getString(R.string.str_share_this_video)));

                }
            });
}
Ivonneivor answered 1/12, 2016 at 7:36 Comment(13)
It gives an error while sharing on WhatsApp - The file format is not supported and for Gmail - Can't attach empty file.Surname
which type of video you want to share ?Ivonneivor
androidcode.ninja/android-share-intent-example check this link thats exactly you wantIvonneivor
Hey, thanks for share, It works for youtube, but not able to share on twitter. anythin else for twitter ?Scoliosis
suv what problem?Ivonneivor
This code is not working for sharing videos via intent so is there any update?Matheson
on which platform you want to share ?Ivonneivor
android oreo(8.0), I have done the required changes for fetching the URI of the video file using FileProvider.Matheson
i mean where do you want to share the video like fb,twiter?Ivonneivor
twitter, whatsapp, etcMatheson
Let us continue this discussion in chat.Matheson
any update or this method won't work for whatsapp?Matheson
@RavishSharma video working fine, but Text is not showingShf
D
1

use this code for pick an video from SD card then send email with video....

    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType("video/3gp");
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Video");
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/dcim/Camera/filename.3gp"));
    sendIntent.putExtra(Intent.EXTRA_TEXT, "Enjoy the Video");
    startActivity(Intent.createChooser(sendIntent, "Email:"));  
Discompose answered 1/12, 2016 at 7:39 Comment(0)
H
1

use this code to share video

fun shareVideo(filePath:String) {

    val videoFile = File(filePath)
    val videoURI = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        FileProvider.getUriForFile(baseContext, baseContext.packageName, videoFile)
    else
        Uri.fromFile(videoFile)
    ShareCompat.IntentBuilder.from(this)
            .setStream(videoURI)
            .setType("video/mp4")
            .setChooserTitle("Share video...")
            .startChooser()



}
Hautevienne answered 6/10, 2019 at 8:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.