如何在android中的SD卡中获取所有图像文件?

时间:2012-04-30 09:23:56

标签: android image android-sdcard

  

可能重复:
  How to get all the pictures from the sdcard of emulator and display it in a listView?

我在sdcard下有这么多文件夹。每个文件夹包含图像文件,文本文件和子文件夹。如何在所有文件夹中获取所有图像文件& SD卡下的子文件夹?我需要将所有图像文件路径存储到一个数组中。任何人都可以帮助我。

1 个答案:

答案 0 :(得分: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;
        }
    }