如何解决如何在andorid中获取所有视频文件
我正在尝试访问手机上的所有视频 onRefresh() 但手机管理器中新添加的视频没有添加到应用程序的 recylerview 中。 OnRefresh(),显示相同的旧视频。这是过程
swipeRefreshLayout.setonRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
getVideos(this);
}
});
@RequiresApi(api = Build.VERSION_CODES.Q)
public List<Items> getVidoes(@NotNull Context context){
List<Items> tempVideos = new ArrayList<>();
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String [] projection = {
MediaStore.Video.Media._ID,MediaStore.Video.Media.DATA,MediaStore.Video.Media.TITLE,};
Cursor cursor = context.getContentResolver().query(uri,projection,null,null);
if (cursor != null){
while (cursor.movetoNext()){
String id = cursor.getString(0);
String path = cursor.getString(1);
String title = cursor.getString(2);
Items items= new Items(id,path,title);
tempVideos.add(videoFiles);
}
cursor.close();
}
return tempVideos;
myAdapter = new MyAdapter(getActivity(),items);
recyclerView.setLayoutManager(new linearlayoutmanager(getContext(),RecyclerView.VERTICAL,false));
recyclerView.setAdapter(myAdapter);
}
我不知道它为什么会这样。感谢您的任何建议。
解决方法
请尝试以下方式使用 Asynchtask doinbackground 并在 onpostexecute 的 recycleview 上显示列表
private String PROJECTION = String(
MediaStore.Files.FileColumns._ID,MediaStore.MediaColumns.DATA,MediaStore.MediaColumns.MIME_TYPE,MediaStore.MediaColumns.WIDTH,MediaStore.MediaColumns.HEIGHT,DURATION
);
Cursor cursor = context.getContentResolver().query(uri,PROJECTION,null,null);
if (cursor != null){
while (cursor.moveToNext()){
long id = cursor.getLong(cursor.getColumnIndexOrThrow(PROJECTION[0]));
String path = ""
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
String uri = QUERY_URI.buildUpon().appendPath(id.toString()).build().toString()
String filePathColumn[] = arrayOf(MediaStore.MediaColumns.DATA);
Cursor pathCursor = contentResolver.query(
Uri.parse(uri),filePathColumn,null
);
pathCursor.moveToFirst();
int columnIndex = pathCursor?.getColumnIndex(filePathColumn[0]) ?: 0 ;
path = pathCursor?.getString(columnIndex) ?: "" ;
pathCursor.close();
} else {
path = cursor.getString(cursor.getColumnIndexOrThrow(PROJECTION[1]));
}
String pictureType =
cursor.getString(cursor.getColumnIndexOrThrow(PROJECTION[2]))
int duration =
cursor.getInt(cursor.getColumnIndexOrThrow(PROJECTION[5]))
int width =
cursor.getInt(cursor.getColumnIndexOrThrow(PROJECTION[3]))
int height =
cursor.getInt(cursor.getColumnIndexOrThrow(PROJECTION[4]))
Item item = Item(0,path,pictureType,width,height)
list.add(item)
}
cursor.close();
}
希望能帮到你
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。