我想从设备上的一个特定路径获取所有歌曲。 在一个例子中,我想从一个特定的文件夹中获取所有歌曲"音乐"来自路径:" / mnt / sdcard / music /" ,我需要在我的代码中更改才能实现此目的? 我有这种方法可以从设备中获取所有歌曲:
public ArrayList<Song> scanAllSongsOnDevice(Context c)
{
ContentResolver musicResolver = c.getContentResolver();
Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String col[] ={android.provider.MediaStore.Audio.Media._ID};
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
if (musicCursor != null && musicCursor.moveToFirst())
{
// clear list to prevent duplicates
songsList = new ArrayList<>();
//get columns
int titleColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.ARTIST);
int isMusicColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.IS_MUSIC);
int duration = musicCursor.getColumnIndex
(MediaStore.Audio.Media.DURATION);
//add songs to list
do
{
String filePath = musicCursor.getString(musicCursor.getColumnIndex(MediaStore.Audio.Media.DATA));
// check if the file is a music and the type is supported
if (musicCursor.getInt(isMusicColumn) != 0 && filePath != null && (FileExtensionFilter.checkValidExtension(filePath)) && musicCursor.getInt(duration) > 0)
{
int thisId = musicCursor.getInt(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
Song song = new Song();
song.setId(thisId);
if(!thisArtist.equals("<unknown>"))
{
song.setArtist(thisArtist);
song.setTitle(thisTitle);
}
else
{
song.setArtist("");
song.setTitle("");
}
song.setSongPath(filePath);
File file = new File(filePath);
song.setFileName(file.getName().substring(0, (file.getName().length() - 4)));
songsList.add(song);
}
}
while (musicCursor.moveToNext());
}
else // if we don't have any media in the folder that we selected set NO MEDIA
{
addNoSongs();
}
musicCursor.close();
if(songsList.size() == 0)
{
addNoSongs();
}
Collections.sort(songsList, new Comparator<Song>()
{
@Override
public int compare(Song song, Song song2)
{
int compare = song.getTitle().compareTo(song2.getTitle());
return ((compare == 0) ? song.getArtist().compareTo(
song2.getArtist()) : compare);
}
});
return songsList;
}
答案 0 :(得分:7)
您需要修改musicCursor
之类的
musicCursor = musicResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,MediaStore.Audio.Media.DATA + " like ? ",
new String[] {"%YOUR_SPECIFIC_FOLDER_NAME%"}, null);
希望它能帮到你......
由于
答案 1 :(得分:1)
首先,您需要通过MediaScanner将您的歌曲数据添加到系统的媒体商店。
String scanDir = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
scanDir = Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator + "music";
} else {
// sdcard is not available
}
MediaScannerConnection.scanFile(getApplicationContext(),
new String[] { scanDir },
new String[] { "audio/*" },
new OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
}
});
这将向框架发送广播意图,框架将扫描您指定的目录并将扫描结果添加到系统媒体商店数据库。
之后,您可以通过contentresolver访问数据,就像您粘贴的代码一样。
答案 2 :(得分:0)
将您的filePath
变量更改为:
String filePath = Environment.getExternalStorageDirectory() + "/music";
答案 3 :(得分:0)
public ArrayList<File> findSong(File root) {
ArrayList<File> al = new ArrayList<File>();
File[] files = root.listFiles(); // All file and folder automatic collect
for (File singleFile : files) {
if (singleFile.isDirectory() && !singleFile.isHidden()) {
al.addAll(findSong(singleFile)); //Recursively call
} else {
if (singleFile.getName().endsWith(".mp3")) {
al.add(singleFile);
}
}
}
return al;
}
的onCreate:
final ArrayList<File> mySongs = findSong(Environment.getExternalStorageDirectory());
items = new String[mySongs.size()];
for (int i = 0; i < mySongs.size(); i++) {
// toast(mySongs.get(i).getName().toString());
// items[i] = mySongs.get(i).getName().toString();
items[i] = mySongs.get(i).getName().toString().replace(".mp3", "");
}