Android MP3文件过滤形式sdcard [英] Android MP3 file Filtering form sdcard

查看:34
本文介绍了Android MP3文件过滤形式sdcard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发从 sd 卡中获取数据的应用程序.

I have developing the application to fetching data from sd card.

我使用此选项进行过滤,但是当我使用 logcat 时,我发现我只获取文件 sdcard/some_dir 的数据

I used this option for filtering but when I used logcat I found that I only get data of only file sdcard/some_dir

但是我将声音文件放入不同的文件夹中,因此如果可能,请给我通用代码以从 sdcard 中获取所有 mp3 文件.

But I have sound files into different folder so if possible give me universal code to fetch all the mp3 file form the sdcard.

class FileExtensionFilter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".mp3") || name.endsWith(".MP3"));
        }
    }

推荐答案

这是我的 AudioFilter :) 希望能帮到你

This is mine AudioFilter :) Hope it will help you

package com.designfuture.music.util;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;

import com.designfuture.framework.util.LogHelper;

public class AudioFileFilter implements FileFilter {

    protected static final String TAG = "AudioFileFilter";
    /**
     * allows Directories
     */
    private final boolean allowDirectories;

    public AudioFileFilter( boolean allowDirectories) {
        this.allowDirectories = allowDirectories;
    }

    public AudioFileFilter() {
        this(true);
    }

    @Override
    public boolean accept(File f) {
        if ( f.isHidden() || !f.canRead() ) {
            return false;
        }

        if ( f.isDirectory() ) {            
            return allowDirectories;
        }
        String ext = getFileExtension(f);
        if ( ext == null) return false;
        try {
            if ( SupportedFileFormat.valueOf(ext.toUpperCase()) != null ) {
                return true;
            }
        } catch(IllegalArgumentException e) {
            //Not known enum value
            return false;    
        }
        return false;
    }

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

    /**
     * Files formats currently supported by Library
     */
    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;
        }
    }

}

这篇关于Android MP3文件过滤形式sdcard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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