简体   繁体   中英

How to detect if a file is audio/video?

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?

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM