【问题标题】:I'm using a cursor to navigate through my internal folders, but the folders are empty?我正在使用光标浏览我的内部文件夹,但文件夹是空的?
【发布时间】:2018-10-08 12:52:36
【问题描述】:

我正在尝试查找内部存储中包含图像的所有文件夹。为此,我正在使用光标。光标不是空的,它是空的,我应该寻找不同的空间来找到这些文件夹吗?

        imageFolders = new ArrayList<String>();

        String[] projection = new String[]{
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                MediaStore.Images.Media.DATE_TAKEN};
        System.out.println("Print progress: Created String Projection");
        System.out.println("Print progress: " + projection[0] + " " + projection[1] + " " + projection[2] + " " + projection[3]);

        Uri imageUri = MediaStore.Images.Media.INTERNAL_CONTENT_URI ;
        System.out.println("Print progress: images uri " + imageUri);
        final String orderBy = android.provider.MediaStore.Images.Media.DATE_TAKEN;
        System.out.println("Print progress: orderBy " + orderBy);

        Cursor cur = getContentResolver().query(imageUri, projection, // Which columns to return
                null, // Which rows to return (all rows)
                null, // Selection arguments (none)
                orderBy + " DESC" // Ordering
        );
        System.out.println("Print progress: cursor created");
        ArrayList<String> imagePath;



        if (cur.moveToFirst()) {
            System.out.println("Print progress: entered cursor"
            );
            String bucket, date;

            int bucketColumn = cur.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
            System.out.println("Print progress: bucketColumn: " + bucketColumn);
            int dateColumn = cur.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN);
            System.out.println("Print progress: dateColumn: " + dateColumn);
            do {
                bucket = cur.getString(bucketColumn);
                System.out.println("Print progress: bucket: " + bucket);

                date = cur.getString(dateColumn);
                System.out.println("Print progress: date: " + date);
                if (!imageFolders.contains(bucket)) {
                    imageFolders.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());
        }
        System.out.println("Print progress: Found the Internal Image Folders");
        return imageFolders;

【问题讨论】:

    标签: java android storage cursors


    【解决方案1】:

    假设 refuses to moveToFirst 你的意思是if 块被跳过,因为moveToFirst() 返回false。当Cursor 为空时会发生这种情况:

    public abstract boolean moveToFirst()

    将光标移动到第一行。如果 光标为空。 - 安卓API.

    【讨论】:

    • 我认为你是对的,我已经适当地修改了这个问题。我仍然对为什么光标为空感到困惑,您有什么见解吗?
    • 简而言之,查询没有返回任何结果。根据 API,query()moveToFirst() 都不会抛出异常,这意味着不清楚您的查询语法是否有错误,或者您要查找的数据是否在您要查找的地方不存在为了它。您只需要进行故障排除即可。欢迎来到软件工程 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 2013-04-14
    • 2010-11-21
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    相关资源
    最近更新 更多