Android:共享意图在视频文件路径上无法正常工作

9

我有一个视频文件路径,想在社交媒体上分享这个视频,但是无法分享。我正在尝试在Android Studio 2.2中使用以下代码,但它不起作用。

代码片段:

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

                }
            }
    );
}

您想展示共享列表。 - Ravish Sharma
有解决方案了吗? - suv
3个回答

5
尝试这个:

试一下:

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

                }
            });
}

2
在WhatsApp分享时出现错误 - 文件格式不受支持,而在Gmail中则无法附加空文件。 - SRK
请查看此链接,这正是您所需的内容:http://www.androidcode.ninja/android-share-intent-example/ - Ravish Sharma
SUV有什么问题? - Ravish Sharma
这段代码无法通过意图分享视频,是否有更新版本? - suv
@RavishSharma 的视频工作正常,但文本未显示。 - Muhammed Haris
显示剩余8条评论

1

使用此代码从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:"));  

1
使用此代码分享视频。
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()



}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接