gpt4 book ai didi

android - 如何从 sd 卡 android 获取所有音频文件

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

我想写一个类,从整个 sd 卡中获取所有的 mp3 文件。但实际上它只是获取直接放在 sd 卡上的音频文件。所以它不会搜索子文件夹。我想使用 Mediastore,但我没有将它放到数组列表中。也许你可以帮助我,非常感谢,很抱歉你是个菜鸟 ;) vinzenz

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;

import de.me.musicplayer.SongsManager.FileExtensionFilter;

import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;




public class SongsManager {

final String MEDIA_PATH = new String("/sdcard/");
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

// Constructor
public SongsManager(){

}

/**
* Function to read all mp3 files from sdcard
* and store the details in ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList(){
File home = new File(MEDIA_PATH);

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 to filter files which are having .mp3 extension
* */
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
}

最佳答案

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

关于android - 如何从 sd 卡 android 获取所有音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19680563/

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