[英]How to find all audio files on SDCard (eclipse, android)
i have already this code...but i want to search also trough the sub folders instead of just searching trough sdcard.sorry for my english ;) thanks for helping, vinzenz 我已经有了这段代码...但是我也想通过子文件夹搜索而不是只通过sdcard.sorry搜索我的英语;)感谢您的帮助,vinzenz
public class SongsManager {
// SDCard Path
final String MEDIA_PATH = new String("/sdcard/");
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// Constructor
public SongsManager(){
**strong text**
}
/**
* 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;
}
} }
This code might help you. 此代码可能对您有所帮助。 It searches for songs in SD card (even in subfolders) & stores the details in
songsList
. 它在SD卡(甚至在子文件夹中)中搜索歌曲,并将详细信息存储在
songsList
。
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
String[] STAR = { "*" };
Cursor cursor;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
cursor = managedQuery(uri, STAR, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String songName = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
String path = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
String albumName = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM));
int albumId = cursor
.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle",albumName+" "+songName+"___"+albumId);
song.put("songPath",path );
songsList.add(song);
} while (cursor.moveToNext());
}
}
Considering that Media is scanned from sdcard, you can get this information from MediaStore 考虑到从SD卡扫描媒体,您可以从MediaStore获取此信息
musiccursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, MediaStore.Audio.Media.MIME_TYPE + "= audio/mpeg", null, null);
int column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
while(musiccursor.moveToNext()){
String path = musiccursor.get(column_index);
//path is your music path, use this
}
there is no direct solution to your question you have to write a piece of code to iterate through each files in a directory, if current file is a directory then list all files of that directory and iterate again on all file of it and so on. 你的问题没有直接的解决方案,你必须编写一段代码来遍历目录中的每个文件,如果当前文件是一个目录,则列出该目录的所有文件并再次迭代它的所有文件,依此类推。
Something like below code will help as in this thread 类似下面的代码将有助于在这个线程
import java.io.File;
public class DirectoryReader {
static int spc_count=-1;
static void process(File aFile) {
spc_count++;
if(aFile.isFile()) {
// your logic to check songs file
} else if (aFile.isDirectory()) {
File[] listOfFiles = aFile.listFiles();
if(listOfFiles!=null) {
for (int i = 0; i < listOfFiles.length; i++) {
process(listOfFiles[i]);
}
} else {
// access is denied
}
}
spc_count--;
}
public static void main(String[] args) {
String nam = "/sdcard/";
File aFile = new File(nam);
process(aFile);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.