Uri.parse("file://" + ???);访问特定文件夹

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

我的问题分为两个部分,我怎样才能得到以下代码来访问这个位置: “/storage/emulated/0/Movies/SpecificFolder”

代码:

private Uri getUriFromMediaStore(int position) {
        int dataIndex = mMediaStoreCursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);

        mMediaStoreCursor.moveToPosition(position);

        String dataString = mMediaStoreCursor.getString(dataIndex);
        Uri mediaUri = Uri.parse("file://" + dataString);
        return mediaUri;
    }

而且,到时候我如何从在线服务器访问 videos/files?

我尝试了以下方法:

mediaCursor = getContentResolver().query( MediaStore.Files.getContentUri("external"), 
                null, 
                MediaStore.Images.Media.DATA + " like ? ", 
                new String[] {"%YOUR_FOLDER_NAME%"}, 
                null);

但是,getContentResolver() 只能在我的 class 扩展 "Activity" 时使用,但它没有,我继承自:

RecyclerView.Adapter<MediaStoreAdapter.ViewHolder>

和Java不允许双重继承。

我弄错了代码的哪一部分需要编辑才能访问特定文件夹。我试图编辑光标显示缩略图的代码部分 chosen/received,而需要做的是限制光标从使用 SQLite 获得 images/videos 的位置。

最初遵循本教程: https://www.youtube.com/watch?v=R23RBPkO8BY&list=PL9jCwTXYWjDJ6o1uabT54z1YmYYBh9US6&index=6

在 Nigel Henshaw 对 Codementors 的帮助下,他能够提供这个解决方案并希望我分享: https://www.codementor.io/mobapptuts

在 MainActivity 中,我们首先设置 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"
        );
    }

指定单个文件夹的代码:

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

此解决方案可处理文件夹中的图像和视频。