我正在使用
创建MediaPlayer实例mPlayer = MediaPlayer.create(getActivity(), Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/soundrecorder/" + arrPackage.get(0)));
此代码在文件名中包含字母'#'时会产生错误。例如:
file path: /storage/emulated/0/soundrecorder/My Recording #1.wav
,但可以很好地适合
file path: /storage/emulated/0/soundrecorder/My Recording 1.wav
我尝试搜索但找不到答案。
04-29 19:54:13.829 31618-31618/com.audiorecorder.wel.voicerecorder D/MediaPlayer: create failed:
java.io.IOException: setDataSource failed.
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1212)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1140)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1074)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1055)
at android.media.MediaPlayer.create(MediaPlayer.java:923)
at android.media.MediaPlayer.create(MediaPlayer.java:900)
at android.media.MediaPlayer.create(MediaPlayer.java:879)
它在Android 4.4.2和Android 5.0.2上给出了此错误,但在Android 7.1.1。
上正常工作。有人可以告诉我这个错误的原因可能是什么?
替换:
Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/soundrecorder/" + arrPackage.get(0))
with:
Uri.fromFile(new File(new File(Environment.getExternalStorageDirectory(), "soundrecorder"), arrPackage.get(0))
看看您是否有更好的运气。您在现有代码中没有使用有效的Uri
(无方案(,并且#
可能会被误处理(URL式编码(。