我试图扫描SD卡中的所有.mp3文件并保存其名称。 这是一个代码片段,负责这一点。 1.我做错了什么? 1. SD卡/ mnt / sdcard或/ SD卡的正确路径是什么?
public class PlayerActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.player_activity);
}
public class SongsManager
{
// SDCard Path
//final String MEDIA_PATH = new String(MediaStore.Audio.Media.getContentUri("external").toString());
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
public SongsManager()
{
}
public ArrayList<HashMap<String, String>> getPlayList()
{
// File home = new File(MEDIA_PATH);
File home = Environment.getExternalStorageDirectory();
if (home.listFiles(new FileExtensionFilter()).length > 0)
{
for (File file : home.listFiles(new FileExtensionFilter()))
{
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
song.put("songPath", file.getPath());
// Adding each song to SongList
songsList.add(song);
}
}
// return songs list array
return songsList;
}
class FileExtensionFilter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
}
}
提前致谢。
答案 0 :(得分:5)
试试此代码段
final String MEDIA_PATH = Environment.getExternalStorageDirectory()
.getPath() + "/";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private String mp3Pattern = ".mp3";
// Constructor
public SongsManager() {
}
/**
* Function to read all mp3 files and store the details in
* ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList() {
System.out.println(MEDIA_PATH);
if (MEDIA_PATH != null) {
File home = new File(MEDIA_PATH);
File[] listFiles = home.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
System.out.println(file.getAbsolutePath());
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
// return songs list array
return songsList;
}
private void scanDirectory(File directory) {
if (directory != null) {
File[] listFiles = directory.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
}
private void addSongToList(File song) {
if (song.getName().endsWith(mp3Pattern)) {
HashMap<String, String> songMap = new HashMap<String, String>();
songMap.put("songTitle",
song.getName().substring(0, (song.getName().length() - 4)));
songMap.put("songPath", song.getPath());
// Adding each song to SongList
songsList.add(songMap);
}
}
答案 1 :(得分:0)
我不确定你的代码有什么问题。但是你可以尝试这种方法:
public HashMap<String, String> scanDirectory(File dir) {
HashMap<String, String> song;
String mp3Pattern = ".mp3";
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
walkdir(listFile[i]);
} else {
if (listFile[i].getName().endsWith(mp3Pattern)){
song = new HashMap<String, String>();
song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
song.put("songPath", file.getPath());
// Adding each song to SongList
songsList.add(song);
}
}
}
}
return song;
}
扫描整个sdcard以进行mp3通话scanDirectory(Environment.getExternalStorageDirectory());
答案 2 :(得分:0)
Environment.getExternalStorageDirectory().getPath()
通常是形式
“/ storage / emulated / 0 /”左右。
你拆分它以获得根 -
String media_path = Environment.getExternalStorageDirectory().getPath();
String[] splitPath= media_path .split("/");
final String MEDIA_PATH = "/" + splitPath[1] + "/";
这有效!
答案 3 :(得分:-1)
问题在于android认为是外部存储。它并不总是您添加到设备的SD卡。内部存储器也可以是外部存储器。 / sdcard或/ mnt / sdcard是相同的位置,它可能指向您的内部存储。所以你必须完全扫描/ mnt文件夹。比如在某些设备中/ mnt / sdcard将是内部存储器和 / mnt / extSdCard将是您添加的那个。 File home = Environment.getExternalStorageDirectory(); 此方法将返回第一个外部存储而不是全部。
答案 4 :(得分:-2)
试试这个: final String MEDIA_PATH =“/ storage /”;