从MediaStore获取制定文件夹下的视频信息

MediaStore是android提供的一个多媒体数据库,在系统重新启动的时候,会主动的去扫描全盘,将所有的音视频,图片文件的信息存储到这个数据库中。android对多有多媒体数据库接口进行了封装,我们可以通过ConentResolver去调用封装好的接口来进行数据库操作。

案例一:

获取“/storage/emulated/0/DCIM/Camera/”下“Vc”开头的文件下的所有视频的修改时间,路径,时长等信息。

private static final String[] VIDEO_PROJECT = { MediaStore.Video.Media._ID, MediaStore.Video.Media.DATE_MODIFIED, MediaStore.Video.Media.DURATION, MediaStore.Video.Media.DATA,MediaStore.Video.Media.DATE_TAKEN };
String selection = MediaStore.Video.Media.DATA+" like ?";
				Cursor cursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, VIDEO_PROJECT, selection, new String[]{"/storage/emulated/0/DCIM/Camera/Vc"+"%"}, MediaColumns.DATE_MODIFIED + " DESC");
				if (cursor != null) {
					int idindex = cursor.getColumnIndex(BaseColumns._ID);
					int modifiedindex = cursor.getColumnIndex(MediaStore.Video.Media.DATE_MODIFIED);
					int durationindex = cursor.getColumnIndex(MediaStore.Video.Media.DURATION);
					int dataindex = cursor.getColumnIndex(MediaStore.Video.Media.DATA);
					int takenindex = cursor.getColumnIndex(MediaStore.Video.Media.DATE_TAKEN);
					while (cursor.moveToNext()) {
						VideoInfo info = new VideoInfo();
						info.setId(cursor.getInt(idindex));
						info.setPath(cursor.getString(dataindex));
						info.setDateModified(cursor.getLong(modifiedindex));
						info.setDateTaken(cursor.getLong(takenindex));
						info.setDuration(cursor.getInt(durationindex));
						mVideoInfos.add(info);
						Log.d("videoinfo", info.toString());
					}
			



ConentResolver中query字段解析:

query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

Uri uri:MediaStore.Video.Media.EXTERNAL_CONTENT_URI     视频所对应的content://

String[] projection:

private static final String[] VIDEO_PROJECT = { MediaStore.Video.Media._ID, MediaStore.Video.Media.DATE_MODIFIED, MediaStore.Video.Media.DURATION, MediaStore.Video.Media.DATA,MediaStore.Video.Media.DATE_TAKEN };

需要查询的字段,不填则查询出所有字段

String selection:

查询条件:参数以?代替,在String[] selectionArgs数组中给出;null则查询所有的

String[] selectionArgs:

查询条件对应的参数;没有则传null


你可能感兴趣的:(android)