我有个问题。我想将我的MP3文件共享给Whatsapp,但是它不起作用!这是我的分享意向:
public void shareAchieve() {
Intent shareAchievement = new Intent();
shareAchievement.setType("audio/*");
shareAchievement.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://de.logtainment.ungesoundboard/" + R.raw.achieve + ".mp3"));
startActivity(Intent.createChooser(shareAchievement, "Teile Längstes Achievement"));
}
但这不起作用!对不起,我的英语不好,我来自德国。
最佳答案
就像@CommonsWare说的一样,很少有应用程序支持android:resource
方案
使您的应用程序与所有应用程序兼容。
您必须通过这种方法将原始资源复制到内部存储
private String CopyRAWtoSDCard(int raw_id,String sharePath) throws IOException {
InputStream in = getResources().openRawResource(raw_id);
FileOutputStream out = new FileOutputStream(sharePath);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
return sharePath;
}
用下面的代码替换您的方法
public void shareAchieve() {
Intent shareAchievement = new Intent(Intent.ACTION_SEND);
shareAchievement.setType("audio/*");
String sPath= null;
try {
sPath = CopyRAWtoSDCard(R.raw.filename, Environment.getExternalStorageDirectory()+"/filename.mp3");
} catch (IOException e) {
e.printStackTrace();
}
Uri uri = Uri.parse(sPath);
shareAchievement.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareAchievement.putExtra(Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(shareAchievement, "Teile Längstes Achievement"));
}
还添加
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
关于android - 我如何获取存储在android的res/raw文件夹中的.mp3文件的URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28928959/