【问题标题】:Share raw resource via WhatsApp通过 WhatsApp 分享原始资源
【发布时间】:2016-08-02 12:57:18
【问题描述】:
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + ContextID.getPackageName() + "/" + ResourceID));
share.setType("audio/*");
ContextID.startActivity(Intent.createChooser(share, "Condividi il suono"));

上面的代码在 Gmail 上运行良好,而 Whatsapp 给出了一个敬酒消息,例如“共享文件失败,请再试一次”

也许我和这个人有同样的问题:Intent.ACTION_SEND Whatsapp

但是我怎样才能暂时将我的资源复制到sd卡上然后共享呢?

【问题讨论】:

    标签: android android-intent resources share whatsapp


    【解决方案1】:
    File dest = Environment.getExternalStorageDirectory();
    InputStream in = ContextID.getResources().openRawResource(ResourceID);              
    
    try 
    {
        OutputStream out = new FileOutputStream(new File(dest, "lastshared.mp3"));  
        byte[] buf = new byte[1024];
        int len;
        while ( (len = in.read(buf, 0, buf.length)) != -1)
        {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
    catch (Exception e) {}              
    
    Intent share = new Intent(Intent.ACTION_SEND);
    share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().toString() + "/lastshared.mp3"));
    share.setType("audio/*");
    ContextID.startActivity(Intent.createChooser(share, "Condividi il suono \"" + TheButton.getText() + "\""));
    return true;
    

    清单:

    <manifest ...>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
        ...
    </manifest>
    

    【讨论】:

    • 您好,我正在使用您的代码通过 WhatsApp 发送 mp3 文件,但收到类似于 Failed to send, please try again 的错误。你知道如何克服这个吗?提前致谢
    • 它的工作..如果您尝试直接从资产共享,whatsapp 将不允许您。
    • 感恩米勒!!干得好;)
    • 您不需要 WRITE_EXTERNAL_STORAGE,因为它可以通过内容提供商完成。通过内容提供商共享您的文件,而不是保存在外部存储中然后共享
    【解决方案2】:

    请从您的代码中更改此行,您将能够分享 把("audio/mp3")写成下面的("audio/*")

    share.setType("audio/mp3");
    

    这是因为whatsapp 的分享类型不支持("audio/*")("*/*")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 2011-05-11
      • 1970-01-01
      相关资源
      最近更新 更多