我想你檢查文件的擴展名來檢查它是否在上面的代碼
public class ReadAllFilesFromPathActivity extends Activity {
private List<String> myList;
File file;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.mylist);
myList = new ArrayList<String>();
File directory = Environment.getExternalStorageDirectory();
file = new File(directory + "/Test");
File list[] = file.listFiles();
for(int i=0; i< list.length; i++)
{
if(checkFileExtension(list[i].getName())
{
myList.add(list[i].getName());
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, myList);
listView.setAdapter(adapter); //Set all the file in the list.
}
}
媒體文件或不添加以下方法檢查擴展。擴展枚舉添加支持的擴展
private boolean checkExtension(String fileName) {
String ext = getFileExtension(fileName);
if (ext == null) return false;
try {
if (SupportedFileFormat.valueOf(ext.toUpperCase()) != null) {
return true;
}
} catch(IllegalArgumentException e) {
return false;
}
return false;
}
public String getFileExtension(String fileName) {
int i = fileName.lastIndexOf('.');
if (i > 0) {
return fileName.substring(i+1);
} else
return null;
}
和枚舉(你也可以添加你自己的)
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;
}
}
這是一個有點更多的代碼,但是可以幫助你
thnks它對我的工作,,,投票了.. – 2015-06-04 12:21:06