/**
* 获取本地所有的视频
*
* @return long
*/
private long getAllLocalVideos() {
LogUtils.libLog(TAG, "getAllLocalVideos");
String[] projection = {
// MediaStore.Video.Media.DATA,
// MediaStore.Video.Media.DISPLAY_NAME,
// MediaStore.Video.Media.DURATION,
MediaStore.Video.Media.SIZE
};
//全部视频
String where = MediaStore.Images.Media.MIME_TYPE + "=? or "
+ MediaStore.Video.Media.MIME_TYPE + "=? or "
+ MediaStore.Video.Media.MIME_TYPE + "=? or "
+ MediaStore.Video.Media.MIME_TYPE + "=? or "
+ MediaStore.Video.Media.MIME_TYPE + "=? or "
+ MediaStore.Video.Media.MIME_TYPE + "=? or "
+ MediaStore.Video.Media.MIME_TYPE + "=? or "
+ MediaStore.Video.Media.MIME_TYPE + "=? or "
+ MediaStore.Video.Media.MIME_TYPE + "=?";
String[] whereArgs =
{"video/mp4", "video/3gp", "video/aiv", "video/rmvb", "video/vob", "video/flv",
"video/mkv", "video/mov", "video/mpg"};
Cursor cursor = getContext().getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
projection, where, whereArgs, MediaStore.Video.Media.DATE_ADDED + " DESC ");
long totalVideoSize = 0;
try {
while (cursor.moveToNext()) {
long size = cursor.getLong(
cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE)); // 大小
totalVideoSize += size;
LogUtils.libLog(TAG, "size:" + size);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close();
}
return totalVideoSize;
}
/*
* @author Hyh
* create at 2019/4/30 10:20
* description: 获取所有图片 并计算大小
*/
private long getAllLocalPhotos() {
LogUtils.libLog(TAG, "getAllLocalPhotos");
String[] projection = {
// MediaStore.Images.Media.DATA,
// MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.SIZE
};
//全部图片
String where = MediaStore.Images.Media.MIME_TYPE + "=? or "
+ MediaStore.Images.Media.MIME_TYPE + "=? or "
+ MediaStore.Images.Media.MIME_TYPE + "=?";
//指定格式
String[] whereArgs = {"image/jpeg", "image/png", "image/jpg"};
//查询
Cursor cursor = getContext().getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, where, whereArgs,
MediaStore.Images.Media.DATE_MODIFIED + " desc ");
long totalPhotoSize = 0;
try {
while (cursor.moveToNext()) {
//获取图片大小
long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.SIZE));
totalPhotoSize += size;
LogUtils.libLog(TAG, "size:" + size);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close();
}
return totalPhotoSize;
}
/*
* @author Hyh
* create at 2019/4/30 10:48
* description: 获取所有音频文件
*
*/
public long getAllLocalAudios() {
LogUtils.libLog(TAG, "getAllLocalAudios");
String[] projection = {
// MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.SIZE
};
String where = MediaStore.Audio.Media.MIME_TYPE + "=? or "
+ MediaStore.Audio.Media.MIME_TYPE + "=?";
String[] whereArgs = {"audio/mpeg", "audio/x-ms-wma"};
Cursor cursor = getContext().getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, where, whereArgs, null);
long totalAudioSize = 0;
try {
while (cursor.moveToNext()) {
long size = cursor.getLong(
cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)); // 大小
totalAudioSize += size;
LogUtils.libLog(TAG, "size:" + size);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close();
}
return totalAudioSize;
}
public long getLocalApps() {
long totalApkSize = 0;
//首先获取到包的管理者
PackageManager packageManager = getContext().getPackageManager();
//获取到所有的安装包
List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0);
for (PackageInfo installedPackage : installedPackages) {
//获取到安装包的路径
String sourceDir = installedPackage.applicationInfo.sourceDir;
File file = new File(sourceDir);
//获取到安装apk的大小
long apkSize = file.length();
totalApkSize += apkSize;
}
return totalApkSize;
}
/*
* @author Hyh
* create at 2019/4/30 10:25
* description: 格式化数值
*/
private String size2string(long size) {
DecimalFormat df = new DecimalFormat("0.00");
String mysize = "";
if (size > 1024 * 1024 * 1024) {
mysize = df.format(size / 1024f / 1024f / 1024f) + "G";
} else if (size > 1024 * 1024) {
mysize = df.format(size / 1024f / 1024f) + "M";
} else if (size > 1024) {
mysize = df.format(size / 1024f) + "K";
} else {
mysize = size + "B";
}
return mysize;
}