分享通过WhatsApp的原始资源 [英] Share raw resource via WhatsApp

查看:182
本文介绍了分享通过WhatsApp的原始资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 意图份额=新的意图(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM,Uri.parse(android.resource://+ ContextID.getPackageName()+/+的ResourceID));
share.setType(音频/ *);
ContextID.startActivity(Intent.createChooser(份额,Condividi IL suono));
 

以上code正常工作与Gmail,WhatsApp的同时,给喜欢共享文件失败,请再试一次举杯消息

也许我的这个家伙同样的问题: Intent.ACTION_SEND WhatsApp的

但如何才能使我暂时复制我的资源在SD卡上,然后分享?

解决方案

 文件DEST = Environment.getExternalStorageDirectory();
InputStream的时间= ContextID.getResources()openRawResource(的ResourceID)。

尝试
{
    的OutputStream OUT =新的FileOutputStream(新文件(DEST,lastshared.mp3));
    byte []的BUF =新的字节[1024];
    INT LEN;
    而((LEN = in.read(buf中,0,buf.length))!=  -  1)
    {
        out.write(BUF,0,的len);
    }
    附寄();
    out.close();
}
赶上(例外五){}

意图份额=新的意图(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM,Uri.parse(Environment.getExternalStorageDirectory()的toString()+/lastshared.mp3));
share.setType(音频/ *);
ContextID.startActivity(Intent.createChooser(份额,Condividi金正日suono \+ TheButton.getText()+\));
返回true;
 

清单:

 <舱单...>
    <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E>< /使用-许可>
    ...
< /舱单>
 

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

The above code works fine with Gmail, while Whatsapp gives a toast message like "Share a file failed, please try it again"

Maybe i've the same problem of this guy: Intent.ACTION_SEND Whatsapp

But how can i temporarily copy my resources on sd card and then share them?

解决方案

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:

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

这篇关于分享通过WhatsApp的原始资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆