赞
踩
一、MediaStore简介
MediaStore
是android
系统提供的一个多媒体数据库,专门用于存放多媒体信息的,通过ContentResolver
即可对数据库进行操作。
MediaStore.Files: 共享的文件,包括多媒体和非多媒体信息
MediaStore.Audio: 存放音频信息
MediaStore.Image: 存放图片信息
MediaStore.Vedio: 存放视频信息
每个内部类中都又包含了Media
,Thumbnails
和相应的MediaColumns
,分别提供了媒体信息,缩略信息和 操作字段。
都是通过 ContentResolver
和 Cursor
来操作的。
-
- /**
- * 获取所有文件
- **/
- public static List<FileEntity> getFilesByType(Context context) {
- List<FileEntity> files = new ArrayList<>();
- // 扫描files文件库
- Cursor c = null;
- try {
- mContentResolver = context.getContentResolver();
- c = mContentResolver.query(MediaStore.Files.getContentUri("external"), null, null, null, null);
- int columnIndexOrThrow_ID = c.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID);
- int columnIndexOrThrow_MIME_TYPE = c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE);
- int columnIndexOrThrow_DATA = c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
- int columnIndexOrThrow_SIZE = c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.SIZE);
- // 更改时间
- int columnIndexOrThrow_DATE_MODIFIED = c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATE_MODIFIED);
-
- int tempId = 0;
- while (c.moveToNext()) {
- String path = c.getString(columnIndexOrThrow_DATA);
- String minType = c.getString(columnIndexOrThrow_MIME_TYPE);
- LogUtil.d("FileManager", "path:" + path);
- int position_do = path.lastIndexOf(".");
- if (position_do == -1) {
- continue;
- }
- int position_x = path.lastIndexOf(File.separator);
- if (position_x == -1) {
- continue;
- }
- String displayName = path.substring(position_x + 1, path.length());
- long size = c.getLong(columnIndexOrThrow_SIZE);
- long modified_date = c.getLong(columnIndexOrThrow_DATE_MODIFIED);
- File file = new File(path);
- String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(file.lastModified()));
- FileEntity info = new FileEntity();
- info.setName(displayName);
- info.setPath(path);
- info.setSize(ShowLongFileSzie(size));
- info.setId((tempId++) + "");
- info.setTime(time);
- files.add(info);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (c != null) {
- c.close();
- }
- }
- return files;
- }
- String[] columns = new String[]{MediaStore.Files.FileColumns._ID, MediaStore.Files.FileColumns.MIME_TYPE, MediaStore.Files.FileColumns
- .SIZE, MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns.DATA};
- c = mContentResolver.query(MediaStore.Files.getContentUri("external"), columns, null, null, null);
- //查找文件夹ScreenRecord下的文件
- c = mContentResolver.query(MediaStore.Files.getContentUri("external"), null, MediaStore.Video.Media.BUCKET_DISPLAY_NAME+"=?", "ScreenRecord", null);
- String select = "(" + MediaStore.Files.FileColumns.DATA + " LIKE '%.doc'" + " or " + MediaStore.Files.FileColumns.DATA + " LIKE '%.docx'" + ")";
- c = mContentResolver.query(MediaStore.Files.getContentUri("external"), null, select , null, null);
c = mContentResolver.query(MediaStore.Files.getContentUri("external"), null, null, null, MediaStore.Files.FileColumns._ID+"DESC");
c = mContentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
c = mContentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
-
- /**
- * 获取视频文件
- **/
- public static List<FileEntity> getFilesByVideo(Context context) {
- List<FileEntity> files = new ArrayList<>();
- // 扫描files文件库
- Cursor c = null;
- try {
- mContentResolver = context.getContentResolver();
- c = mContentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
- int columnIndexOrThrow_ID = c.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
- int columnIndexOrThrow_MIME_TYPE = c.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE);
- int columnIndexOrThrow_DATA = c.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
- int columnIndexOrThrow_SIZE = c.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
- // 更改时间
- int columnIndexOrThrow_DATE_MODIFIED = c.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_MODIFIED);
-
- int tempId = 0;
- while (c.moveToNext()) {
- String path = c.getString(columnIndexOrThrow_DATA);
- String minType = c.getString(columnIndexOrThrow_MIME_TYPE);
- LogUtil.d("FileManager", "path:" + path);
- int position_do = path.lastIndexOf(".");
- if (position_do == -1) {
- continue;
- }
- int position_x = path.lastIndexOf(File.separator);
- if (position_x == -1) {
- continue;
- }
- String displayName = path.substring(position_x + 1, path.length());
- long size = c.getLong(columnIndexOrThrow_SIZE);
- long modified_date = c.getLong(columnIndexOrThrow_DATE_MODIFIED);
- File file = new File(path);
- String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(file.lastModified()));
- FileEntity info = new FileEntity();
- info.setName(displayName);
- info.setPath(path);
- info.setSize(ShowLongFileSzie(size));
- info.setId((tempId++) + "");
- info.setTime(time);
- files.add(info);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (c != null) {
- c.close();
- }
- }
- return files;
- }
- public final Cursor query(
- Uri uri, //数据资源路径
- String[] projection, //查询的列
- String selection, //查询的条件
- String[] selectionArgs, //条件填充值
- String sortOrder){} //排序依据
MediaStore
提供了所有数据库中的字段 宏定义
,操作起来十分方便。
比如:
- INTERNAL_CONTENT_URI; //内部库URI
- EXTERNAL_CONTENT_URI;//外部库URI
- CONTENT_TYPE;//内容提供者类型
- DEFAULT_SORT_ORDER;//排序方式
MediaStore.Files没有EXTERNAL_CONTENT_URI,所以只能用getContentUri()自行获取,得出的URI其实是
Uri.parse("content://media/external/files")
这个不难理解,以MediaStore.Images.Media为例,其URI有三种写法:
- Uri uri1 = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
- Uri uri2 = MediaStore.Images.Media.getContentUri("external");
- Uri uri3 = Uri.parse("content://media/external/images/media");
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。