获取系统所有的音频、视频、图片、应用的大小

 /**
     * 获取本地所有的视频
     *
     * @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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值