您的位置:首页 > 其它

获取SD卡上的全部音频文件

2015-07-19 11:26 260 查看
getFiles("/sdcard/"); // 获取SD卡上的全部音频文件

1.

   private void getFiles(String url) {

        File files = new File(url); // 创建文件对象

        File[] file = files.listFiles();//返回该路径下的所有完整路径的文件名

        try {

            for (File f : file) { // 通过for循环遍历获取到的文件数组

                if (f.isDirectory()) { // 如果是目录,也就是文件夹

                    getFiles(f.getAbsolutePath()); // 递归调用

                } else {

                    if (isAudioFile(f.getPath())) { // 如果是音频文件

                        audioList.add(f.getPath()); // 将文件的路径添加到list集合中

                    }

                }

            }

        } catch (Exception e) {

            e.printStackTrace(); // 输出异常信息

        }

    }

2.

   private static String[] imageFormatSet = new String[] { "mp3", "wav", "3gp" }; // 合法的音频文件格式

    // 判断是否为音频文件

    private static boolean isAudioFile(String path) {

        for (String format : imageFormatSet) { // 遍历数组

            if (path.contains(format)) { // 判断是否为有合法的音频文件

                return true;

            }

        }

        return false;

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: