我在 sdcard 下有很多文件夹。每个文件夹包含图像文件、文本文件和子文件夹。如何获取 sdcard 下所有文件夹和子文件夹中的所有图像文件?我需要将所有图像文件路径存储到一个数组中。谁能帮助我。
问问题
9371 次
1 回答
9
您可以使用以下代码从 SD 卡中搜索所有图像。
private String SD_CARD_ROOT;
File mFile=Environment.getExternalStorageDirectory();
SD_CARD_ROOT=mFile.toString();
private List<String> FindFiles() {
final List<String> tFileList = new ArrayList<String>();
Resources resources = getResources();
// array of valid image file extensions
String[] imageTypes = resources.getStringArray(R.array.image);
FilenameFilter[] filter = new FilenameFilter[imageTypes.length];
int i = 0;
for (final String type : imageTypes) {
filter[i] = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith("." + type);
}
};
i++;
}
FileUtils fileUtils = new FileUtils();
File[] allMatchingFiles = fileUtils.listFilesAsArray(
new File(SD_CARD_ROOT), filter, -1);
for (File f : allMatchingFiles) {
tFileList.add(f.getAbsolutePath());
}
return tFileList;
}
在图像类型的资源数组中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="image">
<item>bmp</item>
<item>cmx</item>
<item>cod</item>
<item>gif</item>
<item>ico</item>
<item>ief</item>
<item>jpe</item>
<item>jpeg</item>
<item>jpg</item>
<item>jfif</item>
<item>pbm</item>
<item>pgm</item>
<item>png</item>
<item>pnm</item>
<item>ppm</item>
<item>ras</item>
<item>rgb</item>
<item>svg</item>
<item>tif</item>
<item>tiff</item>
<item>xbm</item>
<item>xpm</item>
<item>xwd</item>
</string-array>
</resources>
希望这将帮助您找到图像。
这里缺课
public class FileUtils {
public void saveArray(String filename, List<String> output_field) {
try {
FileOutputStream fos = new FileOutputStream(filename);
GZIPOutputStream gzos = new GZIPOutputStream(fos);
ObjectOutputStream out = new ObjectOutputStream(gzos);
out.writeObject(output_field);
out.flush();
out.close();
}
catch (IOException e) {
e.getStackTrace();
}
}
@SuppressWarnings("unchecked")
public List<String> loadArray(String filename) {
try {
FileInputStream fis = new FileInputStream(filename);
GZIPInputStream gzis = new GZIPInputStream(fis);
ObjectInputStream in = new ObjectInputStream(gzis);
List<String> read_field = (List<String>)in.readObject();
in.close();
return read_field;
}
catch (Exception e) {
e.getStackTrace();
}
return null;
}
public File[] listFilesAsArray(File directory, FilenameFilter[] filter,
int recurse) {
Collection<File> files = listFiles(directory, filter, recurse);
File[] arr = new File[files.size()];
return files.toArray(arr);
}
public Collection<File> listFiles(File directory,
FilenameFilter[] filter, int recurse) {
Vector<File> files = new Vector<File>();
File[] entries = directory.listFiles();
if (entries != null) {
for (File entry : entries) {
for (FilenameFilter filefilter : filter) {
if (filter == null
|| filefilter
.accept(directory, entry.getName())) {
files.add(entry);
Log.v("ImageViewFlipper", "Added: "
+ entry.getName());
}
}
if ((recurse <= -1) || (recurse > 0 && entry.isDirectory())) {
recurse--;
files.addAll(listFiles(entry, filter, recurse));
recurse++;
}
}
}
return files;
}
}
于 2012-04-30T09:49:41.800 回答