如何获取包含图片或视频的文件夹的路径

时间:2016-09-14 07:27:39

标签: java android directory gallery photos

我想创建一个Android图库应用。

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

我使用了这段代码并且工作了。但是当我将它与Play商店中的Quickpic Gallery进行比较时,我看到我的应用程序中的文件夹数量少于Quickpic文件夹

您是否在此代码中看到任何问题?

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

    }

2 个答案:

答案 0 :(得分:2)

通过这种方式,您可以找到所有视频和图像父母。

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

查找所有图片文件夹路径         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());
        }
    }

查找所有视频文件夹路径

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

答案 1 :(得分:0)

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

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>对象中的文件夹名称和路径

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

希望这会有所帮助