简体   繁体   English

Android:共享意图不适用于视频文件路径

[英]Android : Share intent is not working for video file path

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.我正在尝试在 Android Studio 2.2 中执行以下代码,但它不起作用。

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"));

                }
            }
    );
}

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)));

                }
            });
}

use this code for pick an video from SD card then send email with video....使用此代码从 SD 卡中选择视频,然后发送带有视频的电子邮件....

    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:"));  

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()



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM