Search code examples
androidandroid-filesubdirectory

How to get list of directory containing images in android


I have used following code to get list of directories

String ROOT_DIR = Environment.getExternalStorageDirectory().getPath();

ROOT_DIR is passed is as string

public static ArrayList<String> getDirectoryPaths(String directory) {
        ArrayList<String> pathArray = new ArrayList<>();
        File file = new File(directory);
        File[] listfiles = file.listFiles();
        if (listfiles != null) {

            for (int i = 0; i < listfiles.length; i++) {
                if (listfiles[i].isDirectory()) {
                    pathArray.add(listfiles[i].getAbsolutePath());
                }
            }

        }
        return pathArray;
    }

`

It returns all directories path but i want to get the path of the directories containing images only like it shows in gallery Eg (Camera, WhatsAppImages, hike,...); I want to get path of all of them.

Any ideas?


Solution

  • Try this:

    Create a Bucket Class

    public class Bucket {
    
        private String name;
        private String firstImageContainedPath;
    
        public Bucket(String name, String firstImageContainedPath) {
            this.name = name;
            this.firstImageContainedPath = firstImageContainedPath;
        }
    
        public String getName() {
            return name;
        }
    
        public String getFirstImageContainedPath() {
            return firstImageContainedPath;
        }
    }
    

    Then, Add this method, it is going to return all the buckets where there are Images.

        public static List<Bucket> getImageBuckets(Context mContext){
                List<Bucket> buckets = new ArrayList<>();
                Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                String [] projection = {MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.Images.Media.DATA};
    
                Cursor cursor = mContext.getContentResolver().query(uri, projection, null, null, null);
                if(cursor != null){
                    File file;
                    while (cursor.moveToNext()){
                        String bucketPath = cursor.getString(cursor.getColumnIndex(projection[0]));
    String fisrtImage = cursor.getString(cursor.getColumnIndex(projection[1]));
                        file = new File(fisrtImage);
                    if (file.exists() && !bucketSet.contains(bucketName)) {
                        buckets.add(new Bucket(bucketName, fisrtImage));
                    }
                    }
                    cursor.close();
                }
                return buckets;
            }
    

    Finally, create your custom spinner item and fill your spinner with an adapter.

    Next Step is to fill the gridview with images from the selected bucket. This method is going to return all the images according the bucketpath.

    public List<String> getImagesByBucket(@NonNull String bucketPath){
    
            Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            String [] projection = {MediaStore.Images.Media.DATA};
            String selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME+" =?";
            String orderBy = MediaStore.Images.Media.DATE_ADDED+" DESC";
    
            List<String> images = new ArrayList<>();
    
            Cursor cursor = mContext.getContentResolver().query(uri, projection, selection,new String[]{bucketPath}, orderBy);
    
            if(cursor != null){
                File file;
                while (cursor.moveToNext()){
                    String path = cursor.getString(cursor.getColumnIndex(projection[0]));
                    file = new File(path);
                    if (file.exists() && !images.contains(path)) {
                        images.add(path);
                    }
                }
                cursor.close();
            }
            return images;
        }
    

    Finally, create your adapter and fill your gridview.

    I hope, It can help you.