Skip to content Skip to sidebar Skip to footer

Get All Audio Files From A Folder

How to get all audio file from a particular folder in android by using the managedQuery(). Means what should be the where clause in managedQuery() to filter the Cursor result. Code

Solution 1:

IS_MUSIC != 0 AND DATA LIKE '/dir/%':

Stringselection= MediaStore.Audio.Media.IS_MUSIC + " != 0 AND " +
            MediaStore.Audio.Media.DATA + " LIKE '/mnt/sdcard/Music/SomeArtist/%'";

This limits your managedQuery results to the .../SomeArtist/ dir.

Solution 2:

I would like you to check extensions of file to check whether it is media file or not in above code

publicclassReadAllFilesFromPathActivityextendsActivity {
privateList<String> myList;
File file;

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.mylist);
myList = newArrayList<String>();

File directory = Environment.getExternalStorageDirectory();
file = newFile( directory + "/Test" );
File list[] = file.listFiles();

for( int i=0; i< list.length; i++)
{
    if(checkFileExtension( list[i].getName() )
    {
         myList.add( list[i].getName() );
    }
}
ArrayAdapter<String> adapter = newArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1, myList);
listView.setAdapter(adapter); //Set all the file in the list.
}
}

and add following method to check extensions. extensions are added in enum

privatebooleancheckExtension(String fileName ) {
    String ext = getFileExtension(fileName);
    if ( ext == null) returnfalse;
    try {
        if ( SupportedFileFormat.valueOf(ext.toUpperCase()) != null ) {
            returntrue;
        }
    } catch(IllegalArgumentException e) {
        returnfalse;    
    }
    returnfalse; 
}

publicStringgetFileExtension(String fileName ) {
    int i = fileName.lastIndexOf('.');
    if (i > 0) {
        return fileName.substring(i+1);
    } elsereturnnull;
}

and enums of supported extensions(you can also add your own)

publicenumSupportedFileFormat
{
    _3GP("3gp"),
    MP4("mp4"),
    M4A("m4a"),
    AAC("aac"),
    TS("ts"),
    FLAC("flac"),
    MP3("mp3"),
    MID("mid"),
    XMF("xmf"),
    MXMF("mxmf"),
    RTTTL("rtttl"),
    RTX("rtx"),
    OTA("ota"),
    IMY("imy"),
    OGG("ogg"),
    MKV("mkv"),
    WAV("wav");

    privateString filesuffix;

    SupportedFileFormat( String filesuffix ) {
        this.filesuffix = filesuffix;
    }

    publicStringgetFilesuffix() {
        return filesuffix;
    }
}

It is a bit more code but may help you

Solution 3:

This is my code which is working with my specific directory path

I made a cursor and with help of that find all song of my directory.

DOWNLOAD_FILE_DIR have the path of my directory

publicstaticfinalStringDOWNLOAD_FILE_DIR= Environment.getExternalStorageDirectory().getPath() + "/MYDIR";

This is Query for find songs of that directory.

Stringselection= MediaStore.Audio.Media.IS_MUSIC + " != 0 AND " +
        MediaStore.Audio.Media.DATA + " LIKE '"+DOWNLOAD_FILE_DIR+"/%'"

this will return a cursor.

context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                null, selection, null, null);

Post a Comment for "Get All Audio Files From A Folder"