【问题标题】:Android : Share intent is not working for video file pathAndroid:共享意图不适用于视频文件路径
【发布时间】:2017-04-15 18:01:26
【问题描述】:

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

代码 sn-p:

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

                }
            }
    );
}

【问题讨论】:

  • 你想显示分享列表
  • 任何解决方案了吗?

标签: java android video android-intent share-intent


【解决方案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()



}

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

    • 在 WhatsApp 上分享时出错 - 文件格式不受支持,对于 Gmail - 无法附加空文件。
    • 你想分享哪种类型的视频?
    • androidcode.ninja/android-share-intent-example 检查这个链接,这正是你想要的
    • 嘿,谢谢分享,它适用于 youtube,但无法在 twitter 上分享。推特还有什么?
    • suv什么问题?
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      相关资源
      最近更新 更多