Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter out only relevant Media files in Android?

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.

like image 927
reiley Avatar asked Feb 11 '14 16:02

reiley


People also ask

How do I get a list of audio files in a specific folder on Android?

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.

Where are media files stored on Android?

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.

What are media files on Android?

Media files are your pictures, music, audios, videos, and documents in fact.


2 Answers

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);
  • Filter by Duration - The duration of the audio file, in ms
  • Filter by Type-Music -Non-zero if the audio file is music
  • Filter by Type-RingTone - Non-zero if the audio file may be a ringtone
  • Filter by Type-Alarm - Non-zero if the audio file may be an alarm

Check for more reference

like image 193
Chintan Khetiya Avatar answered Oct 22 '22 22:10

Chintan Khetiya


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.

like image 25
Edison Avatar answered Oct 22 '22 23:10

Edison