I'm trying to fetch all the music files in my phone:
For this I'm using:
String[] STAR = {"*"};
Uri allExternalSongUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
Cursor cursor = getContentResolver().query(allExternalSongUri, STAR, selection, null, null);
if(cursor != null){
if(cursor.moveToFirst()){
do {
String songName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
Log.i("name", songName);
} while (cursor.moveToNext());
}
cursor.close();
}
But above code, apart from getting music files, is also fetching some additional unnecessary files like *sound_screen_on.mp3* (which is installed & used by some other app).
Issue is my native android music player does not list & plays these unnecessary files.
How can I filter files like these.
id. mylist); myList = new ArrayList<String>(); File directory = Environment. getExternalStorageDirectory(); file = new File( directory + "/Test" ); File list[] = file. listFiles(); for( int i=0; i< list.
On Android, media files are automatically saved in your WhatsApp/Media/folder. If you have Internal Storage, the WhatsApp folder is located in your Internal Storage. If you do not have internal storage, the folder will be on your SD Card or External SD Card.
Media files are your pictures, music, audios, videos, and documents in fact.
You can filter using MediaStore.Audio.AudioColumns class.That will return lots of value of Audio file and you can use it.
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, MediaStore.Audio.Media.DURATION + ">= 60000", null, null);
Check for more reference
Aside from the above methods suggested by others, in the case where column is not reliable from the db, you can always use MediaMetadataRetriever to get the information you want after the first filtering from the db.
http://developer.android.com/reference/android/media/MediaMetadataRetriever.html
This is of course slower than getting existing information from the media db, but it will give you author/album/duration etc other information and you can do as custom filter as you can get with the file list that you get from the ContentProvider.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With