在我的代码中我使用:
mp = new MediaPlayer();
String filePath = Environment.getExternalStorageDirectory().getPath() + "/mymusic/asong.mp3";
try {
mp.setDataSource(filePath);
} catch (IOException e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
在模拟器上,song.mp3正常播放。但是当我在我的真实设备上测试时,它会出错(-38,0)。这意味着它无法找到歌曲的路径。我连接usb线,转到我的电脑,GT-I8260并将文件夹“mymusic”(包含asong.mp3)粘贴到“Card”文件夹下(其中还放置了一个名为“LOST.DIR”的空文件夹)。但为什么它不起作用?非常感谢
答案 0 :(得分:0)
它的卡,但至少
Environment.getExternalStorageDirectory() + "/mymusic/asong.mp3";
就够了。
确保它存在,因为您之前可能没有创建该文件夹。
File f = new File(Environment.getExternalStorageDirectory() + "/mymusic");
if (!f.exists()) { f.mkdirs(); }
还要确保在写作时没有安装,因为它可能根本无法访问。
同时重新验证您已设置了读取/写入外部存储的清单权限