Android存储路径在设备上不一样?

时间:2014-01-27 21:34:10

标签: java android storage

Hej社区! 我想使用File = Environment.getexternaldirectory()或类似的东西从设备的存储中读取mp3文件。 我得到一个空指针exc。当我尝试从那里获取文件时。在我的手机中,我查找了文件浏览器中文件的路径。它说存储\模拟\ 0与我的代码相同。那么,有什么不对? 提前谢谢。

1 个答案:

答案 0 :(得分:1)

使用此方法检索文件列表。

public List<File> getMP3Files(String directory) {
    List<File> files = new ArrayList<File>();
    File folder = new File(directory);
    for (File file : folder.listFiles()) {
        if (file.isFile()) {
            if (file.getName().endsWith(".mp3") || file.getName().endsWith(".MP3")) {
                files.add(file);
            }
        }
    }
    return files;
}

例如,如果您的.mp3文件位于:“/ storage / emulated / 0”,则只需要。

String directoryPath=Environment.getExternalStorageDirectory().getPath(); // path /storage/emulated/0/                  
List<File> list = getMP3Files(directoryPath); 

如果您的.mp3位于其他位置,例如我的.mp3文件位于:“/ storage / emulated / 0 / Music / Overkill”

 String mp3Directory = "/Music/Overkill";
 String directoryPath=Environment.getExternalStorageDirectory().getPath() + mp3Directory; 
 List<File> list = getMP3Files(directoryPath); 

//print in LogCat the list of .mp3:
for (File file : list) {     
        Log.i("MP3 File name", file.getName());
    }