如何获取具有图像或视频的文件夹的路径 [英] How to get paths of folders that has image or video

查看:179
本文介绍了如何获取具有图像或视频的文件夹的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Android画廊应用。

I want to create an android gallery app .

如何扫描和获取包含照片或视频的文件夹的路径。

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

我使用了这段代码并开始工作。但是,当我将其与Play Store中的Quickpic Gallery进行比较时,我发现我的应用程序中的文件夹数量少于Quickpic文件夹

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)) });
        }

    }


推荐答案

我可以看到您正在尝试获取包含视频文件的所有文件夹的文件夹名称,@ prakash ubhadiya给出的答案是一个很好的方法,但是对于这样的问题,如果有很多同名的此类文件夹,该功能将保留只有一个并忽略其余部分,在下面,我修改了他的功能,不仅返回文件夹名称,而且还返回文件夹绝对路径,以防万一您要使用它来获取该特定文件夹中的所有视频文件,我创建了一个类名为 floderFacer 的文件夹包含文件夹名称和文件夹绝对路径,这样一来,所有同名文件夹都不会被忽略,下面是类

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;
    }
} 

下面是修改后的功能,它将返回该文件夹 folderFacer 对象中的名称和路径都在 ArrayList< folderFacer>

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;
    }

希望这会有所帮助

这篇关于如何获取具有图像或视频的文件夹的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆