gpt4 book ai didi

java - 安卓文件扫描

转载 作者:太空宇宙 更新时间:2023-11-04 11:03:37 25 4
gpt4 key购买 nike

我有一个音乐播放器应用程序。我需要扫描手机中的所有音乐文件,但 Android 5.0 及更高版本我无法访问 SD 卡

manifest.xml 中的权限

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

5.0版本之前这部分可以工作

ArrayList<HashMap<String, String>> ist=new ArrayList<>(new getplaylist().getPlayList("/"));

这部分仅返回手机存储而不返回SD卡

        ArrayList<HashMap<String, String>> ist=new ArrayList<>(new
getplaylist().getPlayList(Environment.getExternalStorageDirectory()
.getPath()));

播放列表方法

 public class getplaylist {
public ArrayList<HashMap<String,String>> getPlayList(String rootPath) {
ArrayList<HashMap<String,String>> fileList = new ArrayList<>();
try {
File rootFolder = new File(rootPath);
File[] files = rootFolder.listFiles();
for (File file : files) {
if (file.isDirectory()) {
if (getPlayList(file.getAbsolutePath()) != null) {
fileList.addAll(getPlayList(file.getAbsolutePath()));
} else {
break;
}
} else if (file.getName().endsWith(".mp3")) {
HashMap<String, String> song = new HashMap<>();
song.put("file_path", file.getAbsolutePath());
song.put("file_name", file.getName());
fileList.add(song);
}
}
return fileList;
} catch (Exception e) {
System.out.print(e.toString());
return null;
}
}}

最佳答案

喜欢official android documentation对于 API 级别 21 及以上,表示:

getExternalMediaDirs

Returns absolute paths to application-specific directories on all shared/external storage devices where the application can place media files. These files are scanned and made available to other apps through MediaStore.

在以下位置使用 getExternalMediaDirs 而不是 getExternalStorageDirectory:

 getplaylist().getPlayList(Environment.getExternalStorageDirectory().getPath()));

关于java - 安卓文件扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46645124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com