Android MP3文件过滤表单SD卡
作者:互联网
我正在开发应用程序以从SD卡中获取数据.
我用这个选项进行过滤但是当我使用logcat时我发现我只得到文件sdcard / some_dir的数据
但我有声音文件到不同的文件夹,所以如果可能的话给我通用代码从sdcard获取所有的mp3文件.
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
解决方法:
这是我的AudioFilter :)希望它能帮到你
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;
}
}
}
标签:audio-player,android,android-music-player 来源: https://codeday.me/bug/20190902/1789313.html