通过WhatsApp共享原始资源

时间:2013-08-01 14:06:31

标签: android android-intent resources share whatsapp

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卡上然后分享呢?

2 个答案:

答案 0 :(得分:6)

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>

答案 1 :(得分:0)

请从您的代码中更改此行,您就可以分享了 写下("audio/mp3"),而不是("audio/*")

share.setType("audio/mp3");

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