要从外部存储或相册中显示视频列表在RecyclerView中,你可以按照以下步骤进行操作:
public class Video {
private String title;
private String path;
public Video(String title, String path) {
this.title = title;
this.path = path;
}
public String getTitle() {
return title;
}
public String getPath() {
return path;
}
}
public class VideoAdapter extends RecyclerView.Adapter {
private List
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private VideoAdapter videoAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
List videoList = getVideoListFromStorage(); // 从外部存储或相册中获取视频列表
videoAdapter = new VideoAdapter(videoList);
recyclerView.setAdapter(videoAdapter);
}
private List getVideoListFromStorage() {
List videoList = new ArrayList<>();
// 使用ContentResolver查询MediaStore.Video.Media.EXTERNAL_CONTENT_URI
String[] projection = {MediaStore.Video.Media.TITLE, MediaStore.Video.Media.DATA};
String selection = null;
String[] selectionArgs = null;
String sortOrder = null;
Cursor cursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, sortOrder);
if (cursor != null) {
while (cursor.moveToNext()) {
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.TITLE));
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
videoList.add(new Video(title, path));
}
cursor.close();
}
return videoList;
}
}
现在你可以从外部存储或相册中获取视频列表,并在RecyclerView中显示它们。每个视频条目都会显示视频标题和一个嵌入的视频播放器。