一、MediaStore简介
MediaStore是android系统提供的一个多媒体数据库,专门用于存放多媒体信息的,通过ContentResolver即可对数据库进行操作。
MediaStore.Files: 共享的文件,包括多媒体和非多媒体信息
MediaStore.Audio: 存放音频信息
MediaStore.Image: 存放图片信息
MediaStore.Vedio: 存放视频信息
每个内部类中都又包含了Media,Thumbnails和相应的MediaColumns,分别提供了媒体信息,缩略信息和 操作字段。
二、MediaStore使用
都是通过 ContentResolver 和 Cursor 来操作的。
2.1、MediaStore.Files
2.1.1、查询所有类型文件
/**
* 获取所有文件
**/
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;
}
2.1.2、指定获取的文件字段
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);
2.1.3、根据文件夹的名称查询
//查找文件夹ScreenRecord下的文件
c = mContentResolver.query(MediaStore.Files.getContentUri("external"), null, MediaStore.Video.Media.BUCKET_DISPLAY_NAME+"=?", "ScreenRecord", null);
2.1.4、查询指定类型的文件
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);
2.1.5、指定排序类型,如根据id倒序查询
c = mContentResolver.query(MediaStore.Files.getContentUri("external"), null, null, null, MediaStore.Files.FileColumns._ID+"DESC");
2.2、MediaStore.Audio
2.2.1、查询音频文件
c = mContentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
2.3、MediaStore.Image
2.3.1、查询图片文件
c = mContentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
2.4、MediaStore.Vedio
2.4.1、查询视频文件
/**
* 获取视频文件
**/
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;
}
2.5、query()参数解析:
public final Cursor query(
Uri uri, //数据资源路径
String[] projection, //查询的列
String selection, //查询的条件
String[] selectionArgs, //条件填充值
String sortOrder){} //排序依据
2.6、数据库字段
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");