/**
* Return list of files from path. <FileName, FilePath>
*
* @param path - The path to directory with images
* @return Files name and path all files in a directory, that have ext = "jpeg", "jpg","png", "bmp", "gif"
*/
private List<String> getListOfFiles(String path) {
File files = new File(path);
FileFilter filter = new FileFilter() {
private final List<String> exts = Arrays.asList("jpeg", "jpg",
"png", "bmp", "gif");
@Override
public boolean accept(File pathname) {
String ext;
String path = pathname.getPath();
ext = path.substring(path.lastIndexOf(".") + 1);
return exts.contains(ext);
}
};
final File [] filesFound = files.listFiles(filter);
List<String> list = new ArrayList<String>();
if (filesFound != null && filesFound.length > 0) {
for (File file : filesFound) {
list.add(file.getName());
}
}
return list;
}
Esto le dará la lista de imágenes en una carpeta. Puede modificar el código para obtener todos los archivos.
hey muchas gracias. – sajjoo
¿Ha comprobado el código fuente de los dos métodos 'list()' y 'listFiles()'? Hay una pequeña diferencia entre ellos. Interesante saber cuál es la diferencia. –
@Martjin: internamente, 'listFiles' usa la lista para crear una lista de nombres de archivos y crea objetos' File'. Pero como OP solo quería tener archivos, preferí usar 'listFiles'. –