Uri.parse("file://" + ???);访问特定的文件夹 [英] Uri.parse("file://" + ???); to access specific folder
问题描述
我的问题是两部分,我如何获得以下代码来访问此位置: "/storage/emulated/0/Movies/SpecificFolder"
My question is two part, how can I get the following code to access this location: "/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;
}
,当时间到来时,如何从在线服务器访问视频/文件?
and, when the time comes how can I access videos/files from an online server?
我尝试了以下操作:
mediaCursor = getContentResolver().query( MediaStore.Files.getContentUri("external"),
null,
MediaStore.Images.Media.DATA + " like ? ",
new String[] {"%YOUR_FOLDER_NAME%"},
null);
但是,只有在我的类继承了"Activity",而我却从以下类继承时,才可以使用getContentResolver():
BUT, getContentResolver() can only be used if my class extends "Activity" which it does not, I am inheriting from:
RecyclerView.Adapter<MediaStoreAdapter.ViewHolder>
并且Java不允许双重继承.
and Java does not allow double inheritance.
推荐答案
我误解了代码的哪一部分需要编辑才能访问特定的文件夹.我正在尝试编辑代码部分,其中光标显示其选择/接收的缩略图,而当要做的是限制光标从哪里使用SQLite获取图像/视频时.
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.
最初遵循本教程: https://www.youtube.com/watch?v= R23RBPkO8BY& list = PL9jCwTXYWjDJ6o1uabT54z1YmYYBh9US6& index = 6
在Nigel Henshaw对Codementor的帮助下,他能够提供此解决方案,并希望我分享: https://www.codementor.io/mobapptuts
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
在MainActivity中,我们首先设置了游标:
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.
这篇关于Uri.parse("file://" + ???);访问特定的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!