user6451971
user6451971

Reputation:

How to get paths of folders that has image or video

I want to create an android gallery app .

How to scan and get paths of folders that includes photos or videos .

I used this code and worked . but when i compare it with Quickpic Gallery in play store , i see the count of folders in my app is less than Quickpic folders

Do you see any problem in this code ?

        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    Cursor cursor = ba.context.getContentResolver().query(uri, null, null,
            null, MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME);
    if (cursor != null) {
        cursor.moveToFirst();
        int data = cursor
                .getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        int displayName = cursor
                .getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME);
        imageFolders = new HashMap<>();
        do {
            String imageAddress = cursor.getString(data);
            String imageName = cursor.getString(displayName);
            String folderAddress = imageAddress.substring(0,
                    imageAddress.lastIndexOf(imageName) - 1);

            if (!imageFolders.containsKey(folderAddress)) {
                imageFolders.put(folderAddress, imageAddress);
            }
        } while (cursor.moveToNext());
        for (String str : imageFolders.keySet()) {

            ba.raiseEventFromDifferentThread(
                    null,
                    null,
                    0,
                    "result",
                    true,
                    new Object[] { String.format("%s", str),
                            String.format("%s", imageFolders.get(str)) });
        }

    }

Upvotes: 0

Views: 4043

Answers (2)

Mofor Emmanuel
Mofor Emmanuel

Reputation: 320

i can see you are trying to get the folder names of all folders containing video files the answer given by @prakash ubhadiya is good an works but for the problem that if the are many of such folders with same name the function will keep only one and ignore the rest, below i have modified his fuction to return not only the folder names but also the folder absolute path in case you will want to use this to get all the video files in that specific folder, i have created a class called floderFacer the holds the folder name and the folder adsolute path, done this way no folders with same names will be ignored below is the class

public class folderFacer {

    private String path;
    private String folderName;

    public folderFacer(){

    }

    public folderFacer(String path, String folderName) {
        this.path = path;
        this.folderName = folderName;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getFolderName() {
        return folderName;
    }

    public void setFolderName(String folderName) {
        this.folderName = folderName;
    }
} 

now below is the modified fuction that will return the folder names and paths in a folderFacer object all in an ArrayList<folderFacer>

private ArrayList<folderFacer> getVideoPaths(){
        ArrayList<folderFacer> videoFolders = new ArrayList<>();
        ArrayList<String> videoPaths = new ArrayList<>();
        Uri allVideosuri = android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String[] projection = { MediaStore.Video.VideoColumns.DATA ,MediaStore.Video.Media.DISPLAY_NAME,
                MediaStore.Video.Media.BUCKET_DISPLAY_NAME,MediaStore.Video.Media.BUCKET_ID};
        Cursor cursor = getContentResolver().query(allVideosuri, projection, null, null, null);

        try {
            cursor.moveToFirst();
            do{

                folderFacer folds = new folderFacer();

              String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME));
              String folder = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.BUCKET_DISPLAY_NAME));
              String datapath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));

              String folderpaths =  datapath.replace(name,"");
                if (!videoPaths.contains(folderpaths)) {
                    videoPaths.add(folderpaths);

                    folds.setPath(folderpaths);
                    folds.setFolderName(folder);
                    videoFolders.add(folds);
                }

            }while(cursor.moveToNext());

            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        for(int i = 0;i < videoFolders.size();i++){

            Log.d("video folders",videoFolders.get(i).getFolderName()+" and path = "+videoFolders.get(i).getPath());

        }

        return videoFolders;
    }

hope this helps

Upvotes: 2

prakash ubhadiya
prakash ubhadiya

Reputation: 1271

this way you can find all video and image parents.

ArrayList<String> allFolder;
    HashMap<String, ArrayList<String>> listImageByFolder;
    ArrayList<String> allVideoFolder;
    HashMap<String, ArrayList<String>> listVideoByFolder;

find all images folder path private void getImageFolderList() {

        String[] projection = new String[] { MediaStore.Images.Media.DATA,
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                MediaStore.Images.Media.DATE_TAKEN };
        Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
        Cursor cur = getContentResolver().query(images, projection, // Which
                                                                    // columns
                                                                    // to return
                null, // Which rows to return (all rows)
                null, // Selection arguments (none)
                orderBy + " DESC" // Ordering
        );
        ArrayList<String> imagePath;
        if (cur.moveToFirst()) {
            String bucket, date;
            int bucketColumn = cur.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
            int dateColumn = cur.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN);
            do {
                bucket = cur.getString(bucketColumn);
                date = cur.getString(dateColumn);
                if (!allFolder.contains(bucket)) {
                    allFolder.add(bucket);
                }
                imagePath = listImageByFolder.get(bucket);
                if (imagePath == null) {
                    imagePath = new ArrayList<String>();
                }
                imagePath.add(cur.getString(cur
                        .getColumnIndex(MediaStore.Images.Media.DATA)));
                listImageByFolder.put(bucket, imagePath);
            } while (cur.moveToNext());
        }
    }

find all videos folder path

    private void getVideoFolderList() {
        String[] projection = new String[] { MediaStore.Video.Media.DATA,
                MediaStore.Video.Media._ID,
                MediaStore.Video.Media.BUCKET_DISPLAY_NAME,
                MediaStore.Video.Media.DATE_TAKEN };
        Uri images = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        final String orderBy = MediaStore.Video.Media.DATE_TAKEN;
        Cursor cur = getContentResolver().query(images, projection, // Which
                                                                    // columns
                                                                    // to return
                null, // Which rows to return (all rows)
                null, // Selection arguments (none)
                orderBy + " DESC" // Ordering
        );
        ArrayList<String> imagePath;
        if (cur.moveToFirst()) {
            String bucket, date;
            int bucketColumn = cur.getColumnIndex(MediaStore.Video.Media.BUCKET_DISPLAY_NAME);
            int dateColumn = cur.getColumnIndex(MediaStore.Video.Media.DATE_TAKEN);
            do {
                bucket = cur.getString(bucketColumn);
                date = cur.getString(dateColumn);
                if (!allVideoFolder.contains(bucket)) {
                    allVideoFolder.add(bucket);
                }
                imagePath = listVideoByFolder.get(bucket);
                if (imagePath == null) {
                    imagePath = new ArrayList<String>();
                }
                imagePath.add(cur.getString(cur
                        .getColumnIndex(MediaStore.Images.Media.DATA)));
                listVideoByFolder.put(bucket, imagePath);
            } while (cur.moveToNext());
        }
    }

Upvotes: 2

Related Questions