Question

I have an Android program that needs to list all audio and video files from a given path.

I know how to list all files/folders within a particular folder, but files of other formats may be present in that folder as well. I don't know each and every audio file's format, and other formats may be introduced at a later time. I tried to search for an answer, but to no avail.

How can I detect if a particular file is an audio file?

Was it helpful?

Solution

You can use the below two methods:

private void getallaudio() {
    String[] STAR = {"*"};

    Cursor audioCursor = ((Activity) cntx).managedQuery(
        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, STAR, null, null, null);

    if (audioCursor != null) {
        if (audioCursor.moveToFirst()) {
            do {
                String path = audioCursor.getString(
                    audioCursor.getColumnIndex(MediaStore.Audio.Media.DATA));

                // Log.i("Audio Path",path);
            } while (audioCursor.moveToNext());

        }
        // audioCursor.close();
    }
}

private void getallvideo() {
    String[] STAR = {"*"};

    Cursor videoCursor = ((Activity) cntx).managedQuery(
        MediaStore.Video.Media.EXTERNAL_CONTENT_URI, STAR, null, null, null);

    if (videoCursor != null) {
        if (videoCursor.moveToFirst()) {
            do {
                String path = videoCursor.getString(
                    videoCursor.getColumnIndex(MediaStore.Images.Media.DATA));

                // Log.i("Video Path",path);
            } while (videoCursor.moveToNext());

        }
        // videoCursor.close();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top