4
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 卡上然后共享它们呢?

4

2 回答 2

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>
于 2013-08-01T14:46:20.793 回答
0

请从您的代码中更改此行,您将能够共享("audio/mp3")如下所示的内容("audio/*")

share.setType("audio/mp3");

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

于 2016-08-02T04:57:18.953 回答