[英]How to find all audio files on SDCard (eclipse, android)
我已經有了這段代碼...但是我也想通過子文件夾搜索而不是只通過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;
}
}
此代碼可能對您有所幫助。 它在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());
}
}
考慮到從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
}
你的問題沒有直接的解決方案,你必須編寫一段代碼來遍歷目錄中的每個文件,如果當前文件是一個目錄,則列出該目錄的所有文件並再次迭代它的所有文件,依此類推。
類似下面的代碼將有助於在這個線程
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.