我已经使用这段代码好几年了,它工作得很好:
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所做的任何更改并遇到类似的事情?
试试这个:
Uri uri = Uri.parse(audioPath);
Intent shareIntent = new Intent();
shareIntent.setType("audio/*");
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
我必须通过将声音复制到external-files-dir
来解决此问题。
我不知道为什么whatsapp突然不再接受FileProvider
提供的raw
目录中的文件,而其他应用程序仍然没有任何问题。