سؤال

So this has been keeping me busy all day.

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

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

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

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

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.

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.

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

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...

Thanks in advance.

هل كانت مفيدة؟

المحلول

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

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.

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top