Harmeet Kaur Harmeet Kaur - 1 month ago 18
Android Question

Getting a particular folders images in a directory using cursor loader

I m trying to retrieve images from a particular folder from sd card. what i m able to do is getting all the images from sdcard

code:

onCreateLoader
method:

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = {MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.DATE_ADDED,
MediaStore.Files.FileColumns.MEDIA_TYPE
};
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE;

return new CursorLoader(getContext(), MediaStore.Files.getContentUri("external"), projection, selection, null,
MediaStore.Files.FileColumns.DATE_ADDED + " ASC");
}

Answer

I m trying to retrieve images from a particular folder from sd card

Pass directory name as selectionArgs and change selection String as:

 String selection = MediaStore.Images.Media.DATA + " like ? ";
 String selectionArgs =new String[] {"%PASS_DIR_NAME_HERE%"};

 new CursorLoader(getContext(),MediaStore.Files.getContentUri("external"), 
                 projection, 
                 selection, 
                 selectionArgs,
                 MediaStore.Files.FileColumns.DATE_ADDED + " ASC");