繁体   English   中英

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

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

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

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

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

暂无
暂无

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

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