共享音频文件 [英] Sharing audio file
问题描述
我正在尝试创建一个用于共享音频文件的按钮.这是行不通的.首先,我尝试直接从原始文件夹发送文件,而不将其复制到电话卡中.那没有解决我的问题.我尝试的第二件事是将文件保存到手机,然后共享.将文件保存到手机的部分现在可以工作,但是当我尝试将音频文件共享到另一台设备时,所有兼容的应用程序都会崩溃(Whatsapp,Gmail等).
I'm trying to make a button for sharing an audio file. This is not working. First I tried to send the file right from my raw folder without copying it to the card of the phone. That didn't solve my problem. The second thing I tried, is saving the file to the phone and then share it. The part that saves the file to the phone works now, but when I try to share the audio file to another device all the compatible apps crash (Whatsapp, Gmail, etc).
这是我的代码:
String sharePath = Environment.getExternalStorageDirectory().getPath()
+ "/Soundboard/Ringtones/custom_ringtone.ogg";
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, sharePath);
startActivity(Intent.createChooser(share, "Share Sound File"));
顺便说一句,音频文件是.ogg文件.我希望这些应用程序可以处理这种类型的文件.如果没有,我应该将其转换为.mp3.
By the way, the audio file is a .ogg file. I hope that those apps work with that type of files. If not I should convert it to .mp3.
提前谢谢!
推荐答案
Oké,发现我做错了什么.对于有相同问题的人,这是我解决的方法:
Oké, found out what I did wrong. For people who have the same problem, this is how I solved it:
我忘了将String解析为uri.那就是我必须添加的唯一一行代码. Uri uri = Uri.parse(sharePath);
I forgot to parse the String to an uri. Thats the only line of code I had to add. Uri uri = Uri.parse(sharePath);
这是剩下的全部:
String sharePath = Environment.getExternalStorageDirectory().getPath()
+ "/Soundboard/Ringtones/custom_ringtone.ogg";
Uri uri = Uri.parse(sharePath);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Sound File"));
也不要忘记添加权限WRITE_EXTERNAL_STORAGE
,否则在运行应用程序时会出现错误.
Also do not forget to add permission WRITE_EXTERNAL_STORAGE
otherwise you'll get an error while running your application.
这篇关于共享音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!