Uri.parse(“file://” + ???); to access specific folder

女生的网名这么多〃 提交于 2019-12-02 11:35:30

I was mistaken in which section of the code needed editing to access a specific folder. I was trying to edit the section of code where the cursor shows the thumbnails it has chosen/received, when what needs to be done is to limit where the cursor is getting the images/videos from using SQLite.

Followed this tutorial initially: https://www.youtube.com/watch?v=R23RBPkO8BY&list=PL9jCwTXYWjDJ6o1uabT54z1YmYYBh9US6&index=6

And with Nigel Henshaw's help on Codementors, he was able to provide this solution and wanted me to share: https://www.codementor.io/mobapptuts

In MainActivity, where we first set up the Cursor:

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String[] projection = {
                MediaStore.Files.FileColumns._ID,
                MediaStore.Files.FileColumns.DATE_ADDED,
                MediaStore.Files.FileColumns.DATA,
                MediaStore.Files.FileColumns.MEDIA_TYPE
        };
        String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
                + " OR "
                + MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO
                + MediaStore.Video.Media.DATA;
        return new CursorLoader(
                this.getContext(),
                MediaStore.Files.getContentUri("external"),
                projection,
                selection,
                null,
                MediaStore.Files.FileColumns.DATE_ADDED + " DESC"
        );
    }

The code to specify an individual folder:

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String[] projection = {
                MediaStore.Files.FileColumns._ID,
                MediaStore.Files.FileColumns.DATE_ADDED,
                MediaStore.Files.FileColumns.DATA,
                MediaStore.Files.FileColumns.MEDIA_TYPE
        };
        String selection = "(" + MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE + " AND "
                + MediaStore.Images.Media.DATA + " LIKE ? )"
                + " OR "
                + "(" + MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO + " AND "
                + MediaStore.Video.Media.DATA + " LIKE ? )";
        String [] selectionArgs = new String[] {"%SpecificFolder%", "%SpecificFolder%"};
        return new CursorLoader(
                this.getContext(),
                MediaStore.Files.getContentUri("external"),
                projection,
                selection,
                selectionArgs,
                MediaStore.Files.FileColumns.DATE_ADDED + " DESC"
        );
    }

This solution handles both Images and Videos in your folder.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!