使用 CursorLoader 查询目录 [英] Query directory with CursorLoader

查看:35
本文介绍了使用 CursorLoader 查询目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这让我忙了一整天.

So this has been keeping me busy all day.

我正在自定义一个画廊,用户可以在其中选择一个或多个图像,为此我使用了一个使用 CursorLoader 的现有项目.几乎每个项目都使用媒体库 URI 来查询内存中的所有图像.但是,我只想扫描特定目录并显示其中的图像.

I am customizing a gallery in which the user can select one or multiple images, for which I am using an existing project which makes use of a CursorLoader. Almost every project uses the media gallery URI to query all images on the memory. However, I only want a specific directory to be scanned and the images within displayed.

几行代码可以部分完成我想要的:

Few lines of code which do partly what I want:

final String imagesDirectory = "/storage/sdcard0/DCIM/"
String selection = MediaStore.Images.Media.DATA + " LIKE '" + imagesDirectory + "%'";
cl = new CursorLoader(MultiImageChooserActivity.this, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, img.toArray(new String[img.size()]), selection, null, null);

我在 SO 上也找到了此代码的选择部分,但它还会检索我不想要的子目录中的所有图像.一个解决方案可能是检查 MediaStore.Images.Media.DATA 列是否包含与 imagesDirectory 相同数量的斜杠,这应该有效:

The selection part of this code I found on SO as well, but it also retrieves all images in subdirectories, which I do not want. A solution could be to check whether the the MediaStore.Images.Media.DATA column contains the same amount of slashes as imagesDirectory, which should work:

imagesDirectory: /storage/sdcard0/DCIM/                   -> 4
/storage/sdcard0/DCIM/photo.jpg                           -> 4, direct child
/storage/sdcard0/DCIM/subdir/otherphoto.jpg               -> 5, in subdir

我发现在 SQLite 中,这样的查询应该可以工作(检查/ 的出现):

I found that in SQLite, a query like this should work (to check the occurences of the/):

SELECT LENGTH(col) - LENGTH(REPLACE(col, '/', ''))

其中 col 应该是 MediaStore.Images.Media.DATA,但到目前为止我已经尝试了很多不同的 selection变体,但我还没有设法过滤数据.因为你不能给 CursorLoader 一个 rawQuery(就像一个 Cursor),我也做不到.

in which col should be MediaStore.Images.Media.DATA, but as of yet I've tried a lot of different selection variants, but I haven't managed to filter the data. And since you can't give a CursorLoader a rawQuery (like a Cursor), I couldn't do that either.

我尝试的另一件事是不使用 MediaStore.Images.Media.EXTERNAL_CONTENT_URI 作为内容 URI,而是使用目录中的 URI.所以我尝试了这个:

Another thing I tried was to not use MediaStore.Images.Media.EXTERNAL_CONTENT_URI as content URI, but rather a URI from the directory. So I tried this:

final String imagesDirectory = "/storage/sdcard0/DCIM/"
cl = new CursorLoader(MultiImageChooserActivity.this, 
    Uri.fromFile(new File(imagesDirectory)), 
    img.toArray(new String[img.size()]), null, null, null);

但到目前为止无济于事.

but to no avail thusfar.

问题:如何使用 CursorLoader 查询目录中的所有图像,而不是其子目录?

Question: How do I query all images in a directory, but not its subdirectories, using a CursorLoader?

我不是 SQLite、查询和 CursorLoaders 的英雄,所以这可能会解释一些.另外,我确实搜索了类似的问题,但在这个特定主题上找不到太多东西.即使谷歌搜索 directory cursorloader 也没有回答任何问题.大多数结果是关于按目录排序的SO帖子......

I'm no hero with SQLite, queries and CursorLoaders, so that might explain a little. Also, I did search SO for similar questions, but there's not much to find on this particular topic. Even Googling directory cursorloader does not answer anything. Most result are SO posts about sorting by directory...

提前致谢.

推荐答案

我通过使用扩展子句设法解决了这个问题:

I managed to pull it off by using an extended clause:

String where = "_data LIKE '/storage/sdcard0/DCIM/%' AND (SELECT LENGTH(_data) - LENGTH(REPLACE(_data, '/', ''))) = 4";

其中 /storage/sdcard0/DCIM/ 是我的图像目录,4 是图像目录名称中的斜线数量.我已经在几个不同的目录上尝试过这个(并手动检查它们),它似乎工作得很好.

where /storage/sdcard0/DCIM/ is my image directory and 4 is the amount of slashes in the image directory name. I've tried this on a couple different directories (and manually checking them), and it seems to work just fine.

我也会尝试pskinks方法(DATA like [dir]/%.jpg and DATA not like [dir]/%/%),并报告结果.

I'll try pskinks method as well (DATA like [dir]/%.jpg and DATA not like [dir]/%/%), and report back the result.

这篇关于使用 CursorLoader 查询目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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