在AndroidManifest.xml中添加READ_EXTERNAL_STORAGE权限,并使用ContentResolver查询MediaStore.Video。以下是示例代码:
//检查是否拥有权限 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1); }
//获取视频 String[] projection = { MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION };
Cursor cursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
if (cursor != null) { while (cursor.moveToNext()) { String videoPath = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA)); String videoName = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME)); long videoSize = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.SIZE)); long videoDuration = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DURATION)); //处理获取到的视频信息 } cursor.close(); }