简体   繁体   English

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

[英]Android Storage path not the same on devices?

Hej community! Hej社区! I want to read mp3 files from the storage of the device using File = Environment.getexternaldirectory() or something like this. 我想使用File = Environment.getexternaldirectory()或类似的东西从设备的存储中读取mp3文件。 I get a null pointer exc. 我得到一个空指针exc。 when I try to get Files from there. 当我尝试从那里获取文件时。 In my phone I looked up the path of a file in a file explorer. 在我的手机中,我查找了文件浏览器中文件的路径。 It said storage\\emulated\\0 same as in my code. 它说存储\\模拟\\ 0与我的代码相同。 So, what is wrong? 那么,有什么不对? Thanks in advance. 提前致谢。

Use this method to retrieve a List of files. 使用此方法检索文件列表。

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;
}

for example if you have your .mp3 files located in: "/storage/emulated/0", you only need. 例如,如果你的.mp3文件位于:“/ storage / emulated / 0”,你只需要。

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

If you have your .mp3 in other location, for example i have my .mp3 files in: "/storage/emulated/0/Music/Overkill" 如果您的.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());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM