【发布时间】:2022-04-05 12:21:45
【问题描述】:
我想播放存储卡中的音频文件。
android编程中如何获取外部sdcard路径?
不幸的是,每个安卓手机型号都有不同的路径。例如:
Galaxy S3 android 4.4:存储卡存在路径:“/mnt/extSdCard/”
荣耀7c android 8:内存卡存在路径: "/mnt/ext_sdcard/"
我在诺基亚 5 和 android 9 和小米 Redmi 3s 和 android 6 中找不到路径。
try {
File root = Environment.getExternalStorageDirectory();
String audioPath = root.getAbsolutePath() + "/telavatquran/naba.mp3";
FileInputStream fis = new FileInputStream(audioPath);
fd = fis.getFD();
if (fd != null) {
player.setDataSource(fd);
player.prepare();
player.start();
} else if (new File("/mnt/sdcard/external_sd/").exists()) {
seekUpdation();
player.setDataSource("/mnt/sdcard/external_sd/telavatquran/naba.mp3/");
player.prepare();
player.start();
} else if (new File("/storage/extSdCard/").exists()) {
seekUpdation();
player.setDataSource("/storage/extSdCard/telavatquran/naba.mp3/");
player.prepare();
player.start();
} else if (new File("/mnt/extSdCard/").exists()) {
seekUpdation();
player.setDataSource("/mnt/extSdCard/telavatquran/naba.mp3/");
player.prepare();
player.start();
} else if (new File("/mnt/sdcard/external_sd/").exists()) {
seekUpdation();
player.setDataSource("/mnt/sdcard/external_sd/telavatquran/naba.mp3/");
player.prepare();
player.start();
} else if (new File("storage/sdcard1/").exists()) {
seekUpdation();
player.setDataSource("storage/sdcard1/telavatquran/naba.mp3/");
player.prepare();
player.start();
} else if (new File("/mnt/ext_sdcard/").exists()) {
seekUpdation();
player.setDataSource("/mnt/ext_sdcard/telavatquran/naba.mp3/");
player.prepare();
player.start();
}
} catch (Exception e) {
}
感谢您的合作。
【问题讨论】:
-
"android编程中如何获取外部sdcard路径?" ——你没有。您无权访问removable storage 上的任意位置。而且,在 Android 10 及更高版本上,我们是losing access to most user-facing storage, in terms of filesystem APIs。 “我想播放音频文件”——然后查询
MediaStore获取音频内容。使用Uri值播放它。 -
这能回答你的问题吗? How to get audio path to play audio file?
-
这能回答你的问题吗? Find location of a removable SD card