如何检测如果文件是音频/视频? [英] How to detect if a file is audio/video?

查看:196
本文介绍了如何检测如果文件是音频/视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要从给定路径列出所有的音频和视频文件的Andr​​oid程序。

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

我知道如何列出所有文件/其它格式的特定文件夹内的文件夹,但文件可能是present该文件夹中也是如此。我不知道每个音频文件的格式和其他格式可以在以后的时间被引入。我试图寻找一个答案,但无济于事。

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

这篇关于如何检测如果文件是音频/视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆