Images from a folder on an SD card

I have an application that needs to read images from the folder created by the application on the SD card (sdcard / "foldername" / "filename.jpg". I have no idea what file names are because the user specifies the file names. I need read the images from the folder and do something like viewing the default images. Im thinking of reading them first in the form of a grid, but 1) I can’t figure out how to dynamically read them from the folder 2) how would I use image settings such as image viewer by default? If there was a way to open the default viewer in a specific folder that would help. any input would be awesome, working on it for a while. thanks

+4
source share
1 answer

Here you can get the list of folders from the memory card:

String state = Environment.getExternalStorageState(); if(state.contentEquals(Environment.MEDIA_MOUNTED) || state.contentEquals(Environment.MEDIA_MOUNTED_READ_ONLY)) { String homeDir = Environment.getExternalStorageDirectory(); File file = new File(homeDir); File[] directories = file.listFiles(); } else { Log.v("Error", "External Storage Unaccessible: " + state); } 

This code is from the top of my head, so some syntax may be a bit, but the general idea should work. You can use something like this to filter folders only in folders containing images:

 FileFilter filterForImageFolders = new FileFilter() { public boolean accept(File folder) { try { //Checking only directories, since we are checking for files within //a directory if(folder.isDirectory()) { File[] listOfFiles = folder.listFiles(); if (listOfFiles == null) return false; //For each file in the directory... for (File file : listOfFiles) { //Check if the extension is one of the supported filetypes //imageExtensions is a String[] containing image filetypes (eg "png") for (String ext : imageExtensions) { if (file.getName().endsWith("." + ext)) return true; } } } return false; } catch (SecurityException e) { Log.v("debug", "Access Denied"); return false; } } }; 

Then change the first example to:

File[] directories = file.listFiles(filterForImageFolders);

This should only return directories containing images. Hope this helps some!

+7
source

All Articles