这是很简单的功能,但之所以还有这个话题。是因为,华为的手机手机上,Width记录的是0.
后来发现还有RESOLUTION这个字段。存储的格式:1920x1080, 用x来分割宽高, 后来又发现sony的手机上有的是小写x,有的是大写X.
以上情况的存在,导致Mediastore里取视频信息就复杂了一点, 性能也下降很多。
像获取符合条件的视频个数,就麻烦很多:
要用下面的方式:
public static int getVideoCount(int minWidth, int maxWidth) {
int count = 0;
Cursor fileCursor = null;
try {
fileCursor = BaseApplication.getInstance().getApplicationContext().getContentResolver().query(MediaStore.Video.Media.getContentUri("external"), new String[]{MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.RESOLUTION}, null, null, MediaStore.MediaColumns.SIZE + " DESC");
if (null != fileCursor && fileCursor.moveToNext()) {
do {
int columnIndex = fileCursor.getColumnIndex(MediaStore.MediaColumns.DATA);
int columnResolutionIndex = fileCursor.getColumnIndex(MediaStore.MediaColumns.RESOLUTION);
String resolution = fileCursor.getString(columnResolutionIndex);
if (!TextUtils.isEmpty(resolution)) {
resolution = resolution.replace("×","x");
String[] resolutions = resolution.split("x");
if (resolutions != null && resolutions.length == 2) {
int widthValue = Integer.valueOf(resolutions[0]);
File file = new File(fileCursor.getString(columnIndex));
if (file.isFile() && file.exists() && !file.isHidden()) {
if (widthValue >= minWidth && widthValue <= maxWidth) {
count++;
}
}
}
}
}
while (fileCursor.moveToNext());
}
} catch (Exception e) {
} finally {
if (fileCursor != null) {
fileCursor.close();
}
}
return count;
}