Reputation: 47
I want to get all the songs from one specific path on the device. in a example i want to get all the songs from one specific folder "music" from path: "/mnt/sdcard/music/" , what i need to change in my code to able to achieve this? i have this method that get all the songs from the device:
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;
}
Upvotes: 3
Views: 5591
Reputation: 579
You need to modify your audioCursor
like
audioCursor = audioResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,MediaStore.Audio.Media.DATA + " like ? ",
new String[] {"%YOUR_SPECIFIC_FOLDER_NAME%"}, null);
Hope it will help you...
Thanks
Upvotes: 9
Reputation: 41
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", "");
}
Upvotes: -1
Reputation: 975
Change your filePath
variable to this:
String filePath = Environment.getExternalStorageDirectory() + "/music";
Upvotes: -1
Reputation: 12167
Firstly you need to add the data of you songs to system's media store by 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) {
}
});
This will send a broadcast intent to framework, the framework will scan the directory you specify and add the scanned result to system media store database.
After that, you can access the data by contentresolver like the code you pasted.
Upvotes: 0