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

2019-03-04 11:56发布

问题:

My question is two part, how can I get the following code to access this location: "/storage/emulated/0/Movies/SpecificFolder"

Code:

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

and, when the time comes how can I access videos/files from an online server?

I have tried the following:

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

BUT, getContentResolver() can only be used if my class extends "Activity" which it does not, I am inheriting from:

RecyclerView.Adapter<MediaStoreAdapter.ViewHolder>

and Java does not allow double inheritance.

回答1:

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.