2

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 is:

String[] proj = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DURATION };
Cursor cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, /*where clause here*/, null, null);
user1041858
  • 947
  • 2
  • 15
  • 32

3 Answers3

6

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

String selection = 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.

Yegor
  • 2,514
  • 2
  • 19
  • 27
2

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

public class ReadAllFilesFromPathActivity extends Activity {
private List<String> myList;
File file;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.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.length; i++)
{
    if(checkFileExtension( list[i].getName() )
    {
         myList.add( list[i].getName() );
    }
}
ArrayAdapter<String> adapter = new ArrayAdapter<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

private boolean checkExtension( String fileName ) {
    String ext = getFileExtension(fileName);
    if ( ext == null) return false;
    try {
        if ( SupportedFileFormat.valueOf(ext.toUpperCase()) != null ) {
            return true;
        }
    } catch(IllegalArgumentException e) {
        return false;    
    }
    return false; 
}

public String getFileExtension( String fileName ) {
    int i = fileName.lastIndexOf('.');
    if (i > 0) {
        return fileName.substring(i+1);
    } else 
        return null;
}

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

public enum SupportedFileFormat
{
    _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");

    private String filesuffix;

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

    public String getFilesuffix() {
        return filesuffix;
    }
}

It is a bit more code but may help you

Akbari Dipali
  • 2,173
  • 1
  • 20
  • 26
1

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

public static final  String DOWNLOAD_FILE_DIR = Environment.getExternalStorageDirectory().getPath() + "/MYDIR";

This is Query for find songs of that directory.

String selection = 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);
Otieno Rowland
  • 2,182
  • 1
  • 26
  • 34
ajay singh
  • 169
  • 1
  • 5