码迷,mamicode.com
首页 > 其他好文 > 详细

StorageUtil

时间:2020-06-16 15:06:46      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:public   text   util   ice   constant   androi   dem   容量   private   

package com.android.demo.lileidemo.utils;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

import com.android.demo.lileidemo.constant.AppConstants;

import java.io.File;

/**
* date: 04/08/2020.
* author: lilei.
*/
public class StorageUtil {
private static final String TAG = AppConstants.APP_TAG + "StorageUtil ";
private static final boolean DEBUG = false;
private static volatile StorageUtil mInstance;

public static StorageUtil getInstance() {
if (null == mInstance) {
synchronized (StorageUtil.class) {
if (null == mInstance) {
mInstance = new StorageUtil();
}
}
}
return mInstance;
}

public String checkSD(Context contect) {

File data = new File("/data");
Environment.getDataDirectory(); //获得data的路径
StatFs data_stat = new StatFs(data.getPath()); //创建StatFs对象
Log.d(TAG, "checkSD() data.getPath():" + data.getPath());
long data_blockSize = data_stat.getBlockSize(); //获取block的size
float data_totalBlocks = data_stat.getBlockCount();//获取block的个数
float data_totalInMb = (float) ((data_blockSize * data_totalBlocks) / 1024 / 1024);//计算总容量
long data_availableBlocks = data_stat.getAvailableBlocks(); //获取可用block的个数
float data_available = (float) ((data_blockSize * data_availableBlocks) / 1024 / 1024);//计算可用容量
String notice = "FLASH使用情况:\n总容量:" + data_totalInMb + "M.\n已用:" + (data_totalInMb - data_available + "\n可用:" + data_available + "M.");
//很多情况下data分区就是sdcard
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File filePath = Environment.getExternalStorageDirectory(); //获得sd 卡的路径

StatFs stat = new StatFs(filePath.getPath()); //创建StatFs对象
long blockSize = stat.getBlockSize(); //获取block的size
float totalBlocks = stat.getBlockCount(); //获取block的个数
int sizeInMb = (int) (blockSize * totalBlocks) / 1024 / 1024; //计算总容量
long availableBlocks = stat.getAvailableBlocks(); //可用block的个数
float percent = (int) (blockSize * availableBlocks) / 1024 / 1024; //计算可用容量

notice = notice + "\nSD卡使用情况:\n总容量:" + sizeInMb + "M.\n已用:" + (sizeInMb - percent + "M\n可用:" + percent + "M.");
return notice;
} else {
notice = notice + "\nSD卡使用情况:未插入SD卡";
return notice;
}

}
}

StorageUtil

标签:public   text   util   ice   constant   androi   dem   容量   private   

原文地址:https://www.cnblogs.com/adamli/p/13140725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!