如何存取权限的SD卡,并返回和数组的文件从一个特定的格式? [英] How to acces sdcard and return and array with files off a specific format?
问题描述
我需要存取权限的SD卡,并返回不同格式的一些文件。的位置将是由用户输入。我怎样才能做到这一点编程?
I need to acces the sdcard and return some files of different formats. The location will be input by the user. How can I do this programmatically?
推荐答案
Simondid, 我相信这是你所期待的。
Simondid, I believe this is what you are looking for.
访问SD卡: <一href="http://stackoverflow.com/questions/3779944/reading-a-specific-file-from-sdcard-in-android">reading从SD卡中的Android 一个特定的文件
请注意检查介质的可用性: 的http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
创建文件筛选器: 的http://www.devdaily.com/blog/post/java/how-implement-java-filefilter-list-files-directory
一个MP3文件过滤器的示例,创建以下过滤器类:
Example of a mp3 file filter, Create the following filter class:
import java.io.*;
/**
* A class that implements the Java FileFilter interface.
* It will filter and grab only mp3
*/
public class Mp3FileFilter implements FileFilter
{
private final String[] okFileExtensions =
new String[] {"mp3"};
public boolean accept(File file)
{
for (String extension : okFileExtensions)
{
if (file.getName().toLowerCase().endsWith(extension))
{
return true;
}
}
return false;
}
}
在此基础上访问你会用这样的过滤器中的SD卡的早期的岗位:
File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard, "path/to/the/directory/with/mp3");
//THIS IS YOUR LIST OF MP3's
File[] mp3List = dir.listFiles(new Mp3FileFilter());
注:code是粗糙的,你可能会想,以确保SD卡可作为上述
这篇关于如何存取权限的SD卡,并返回和数组的文件从一个特定的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!