我使用此代码已有好几年了,并且运行良好:
final Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sharingIntent.setType("audio/mpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM,
SoundProvider.getUriForSound(getContext(), sound));
getActivity()
.startActivity(Intent.createChooser(sharingIntent,
getContext().getString(R.string.share)));
我的SoundProvider
生成一个以content://
开头的URI,该URI由FileProvider
(实际上是相同的SoundProvider
)拾取。该提供程序从我的raw
文件夹中读取音频文件。
声音可以在WhatsApp中直接播放(而不是通用文件),并以ID3标签中的正确标题显示。
这已经完美地起作用了,但仍然适用于Telegram / Dropbox等。但是直到几个月前WhatsApp的最新更新才失败,并显示消息“共享失败,请重试”。
有人知道WhatsApp所做的任何更改并且遇到类似的情况吗?
答案 0 :(得分:0)
尝试一下:
Uri uri = Uri.parse(audioPath);
Intent shareIntent = new Intent();
shareIntent.setType("audio/*");
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
答案 1 :(得分:0)
我不得不通过将声音复制到external-files-dir
来解决此问题。
我不知道为什么whatsapp突然不再接受raw
服务的FileProvider
目录中的文件,而其他应用仍然没有问题。