How To Pick All Files From Folders And Subfolders In Android?
Possible Duplicate: Recursively list files in Java I have created one folder named SlideShow in sdcard. Inside this folder i created two folders namely folder1 and folder2. Thes
Solution 1:
I think that you can use recrusive function like this: you can take help through this example
publicList<String> images=new ArrayList<String>();
public recursiveFunction(File dirPath) {
File f = new File(dirPath);
File[] files = f.listFiles();
for(int i=0;i,**files.length - 1**;i++)
{
if(files[position].isFile())
{
int mid= files[position].getName().lastIndexOf(".");
String ext=files[position].getName().substring(mid+1,files[position].getName().length());
if( ext.equalsIgnoreCase("jpg")
|| ext.equalsIgnoreCase("png")
|| ext.equalsIgnoreCase("jpeg")
|| ext.equalsIgnoreCase("gif"))
{
images.add(files[position].getAbsoluteFile();
}
}
else
recursiveFunction(files[position].getAbsoluteFile());
}
}
in images list you have your all images.
Solution 2:
Try with the following code to get all images from SlideShow folder and its sub folder
publicstatic ArrayList<String> getPathOfAllImages(Activity activity) {
trimCache();
String absolutePathOfImage = null;
String nameOfFile = null;
String absolutePathOfFileWithoutFileName = null;
Uri uri;
Cursor cursor;
int column_index;
int column_displayname;
int lastIndex;
// absolutePathOfImages.clear();
ArrayList<String> absolutePathOfImageList = new ArrayList<String>();
if (absolutePathOfImageList.isEmpty()) {
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaColumns.DATA,
MediaColumns.DISPLAY_NAME };
cursor = activity.managedQuery(uri, projection, null, null, null);
column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
column_displayname = cursor
.getColumnIndexOrThrow(MediaColumns.DISPLAY_NAME);
// cursor.moveToFirst();while (cursor.moveToNext()) {
// for(int i=0; i<cursor.getColumnCount();i++){// Log.i(TAG,cursor.getColumnName(i)+".....Data Present ...."+cursor.getString(i));// }// Log.i(TAG,"=====================================");
absolutePathOfImage = cursor.getString(column_index);
nameOfFile = cursor.getString(column_displayname);
lastIndex = absolutePathOfImage.lastIndexOf(nameOfFile);
lastIndex = lastIndex >= 0 ? lastIndex
: nameOfFile.length() - 1;
absolutePathOfFileWithoutFileName = absolutePathOfImage
.substring(0, lastIndex);
if (!((absolutePathOfFileWithoutFileName.equals(Environment
.getExternalStorageDirectory() + "/SlideShow/")))) {
if (absolutePathOfImage != null) {
absolutePathOfImageList.add(absolutePathOfImage);
}
}
}
}
// Log.i(TAG,"........Detected images for Grid....."// + absolutePathOfImageList);return absolutePathOfImageList;
}
Post a Comment for "How To Pick All Files From Folders And Subfolders In Android?"