【问题标题】:How can i send either an image or a video file using ACTION_SEND?如何使用 ACTION_SEND 发送图像或视频文件?
【发布时间】:2015-06-30 01:20:36
【问题描述】:

我想使用ACTION_SEND 共享图像或视频文件。所以基本上当用户点击图像并选择“共享图像/视频”时,它应该发送选择的图像或选择的视频。

这是我正在使用的代码:

if (filep != null) {

      }
      File sending=new File(filep);
      Intent intent = new Intent();
      intent.setAction(android.content.Intent.ACTION_SEND);
      intent.setDataAndType(Uri.fromFile(sending),getMimeType(sending.getAbsolutePath()));
      intent.putExtra(Intent.EXTRA_STREAM, sending);
      startActivity(Intent.createChooser(intent , "Share"));
    }

  private String getMimeType(String url)
    {
        String parts[]=url.split("\\.");
        String extension=parts[parts.length-1];
        String type = null;
        if (extension != null) {
            MimeTypeMap mime = MimeTypeMap.getSingleton();
            type = mime.getMimeTypeFromExtension(extension);
        }
        return type;

因此,在测试时,我需要使用哪个应用程序来与WhatsApp,Facebook,电子邮件等共享,然后选择任何一个应用程序,然后说“共享失败,请重试”。我似乎无法弄清楚为什么它不起作用。但是,我有相同的代码来使用ACTION_VIEW 全屏显示图像或视频文件,这似乎很好用,但不适用于共享。

有人可以帮忙吗?

【问题讨论】:

    标签: android image video bitmap


    【解决方案1】:

    EXTRA_STREAM 必须是 Uri,并且您没有传递 Uri。使用Uri.fromFile() 构造一个Uri。

    另外,将setDataAndType() 替换为setType(),如ACTION_SEND does not use the data aspect of the Intent。

    【讨论】:

      猜你喜欢
      • 2021-02-23
      • 1970-01-01
      • 2013-07-07
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 2022-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多