下面列出了android.os.StatFs#getBlockSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Gets the information about the free storage space, including reserved blocks, either internal
* or external depends on the given input
*
* @param storageType Internal or external storage type
* @return available space in bytes, -1 if no information is available
*/
@SuppressLint("DeprecatedMethod")
public long getFreeStorageSpace(StorageType storageType) {
ensureInitialized();
maybeUpdateStats();
StatFs statFS = storageType == StorageType.INTERNAL ? mInternalStatFs : mExternalStatFs;
if (statFS != null) {
long blockSize, availableBlocks;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSize = statFS.getBlockSizeLong();
availableBlocks = statFS.getFreeBlocksLong();
} else {
blockSize = statFS.getBlockSize();
availableBlocks = statFS.getFreeBlocks();
}
return blockSize * availableBlocks;
}
return -1;
}
/**
* 判断当前ROM空间是否足够大
*/
@Deprecated
public static boolean checkRomSpaceEnough(long limitSize) {
long allSize;
long availableSize = 0;
try {
File data = Environment.getDataDirectory();
StatFs sf = new StatFs(data.getPath());
availableSize = (long) sf.getAvailableBlocks() * (long) sf.getBlockSize();
allSize = (long) sf.getBlockCount() * (long) sf.getBlockSize();
} catch (Exception e) {
allSize = 0;
}
if (allSize != 0 && availableSize > limitSize) {
return true;
}
return false;
}
/**
* Gets the information about the total storage space, either internal or external depends on the
* given input
*
* @param storageType Internal or external storage type
* @return available space in bytes, -1 if no information is available
*/
@SuppressLint("DeprecatedMethod")
public long getTotalStorageSpace(StorageType storageType) {
ensureInitialized();
maybeUpdateStats();
StatFs statFS = storageType == StorageType.INTERNAL ? mInternalStatFs : mExternalStatFs;
if (statFS != null) {
long blockSize, totalBlocks;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSize = statFS.getBlockSizeLong();
totalBlocks = statFS.getBlockCountLong();
} else {
blockSize = statFS.getBlockSize();
totalBlocks = statFS.getBlockCount();
}
return blockSize * totalBlocks;
}
return -1;
}
/**
* Gets the information about the available storage space either internal or external depends on
* the give input
*
* @param storageType Internal or external storage type
* @return available space in bytes, 0 if no information is available
*/
@SuppressLint("DeprecatedMethod")
public long getAvailableStorageSpace(StorageType storageType) {
ensureInitialized();
maybeUpdateStats();
StatFs statFS = storageType == StorageType.INTERNAL ? mInternalStatFs : mExternalStatFs;
if (statFS != null) {
long blockSize, availableBlocks;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSize = statFS.getBlockSizeLong();
availableBlocks = statFS.getAvailableBlocksLong();
} else {
blockSize = statFS.getBlockSize();
availableBlocks = statFS.getAvailableBlocks();
}
return blockSize * availableBlocks;
}
return 0;
}
/**
* 计算SD卡的剩余空间
*
* @return 返回-1,说明没有安装sd卡
*/
public static long getFreeDiskSpace() {
String status = Environment.getExternalStorageState();
long freeSpace = 0;
if (status.equals(Environment.MEDIA_MOUNTED)) {
try {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
freeSpace = availableBlocks * blockSize / 1024;
} catch (Exception e) {
e.printStackTrace();
}
} else {
return -1;
}
return (freeSpace);
}
/**
* Checks if given dictionary file exists. If not, user is prompted for a
* download and the files are downloaded if requested.
* @param activity context
* @param downloader the downloader implementation
* @param skipMissingMsg if true then the "dictionary is missing" message is shown only when there is not enough free space.
* @return true if the files are available, false otherwise.
*/
private boolean checkDictionaryFile(final Activity activity, final AbstractDownloader downloader, final boolean skipMissingMsg) {
if (!isComplete(downloader.targetDir)) {
final StatFs stats = new StatFs("/sdcard");
final long free = ((long) stats.getBlockSize()) * stats.getAvailableBlocks();
final StringBuilder msg = new StringBuilder(AedictApp.format(R.string.dictionary_missing_download, downloader.dictName));
if (free < downloader.expectedSize) {
msg.append('\n');
msg.append(AedictApp.format(R.string.warning_less_than_x_mb_free, downloader.expectedSize / 1024, free / 1024));
}
if (free >= downloader.expectedSize && skipMissingMsg) {
download(downloader);
activity.startActivity(new Intent(activity, DownloadActivity.class));
} else {
new DialogActivity.Builder(activity).setDialogListener(new DownloaderDialogActivity(downloader)).showYesNoDialog(msg.toString());
}
return false;
}
return true;
}
/**
* See {@link android.os.StatFs#getBlockSize}.
*/
@SuppressWarnings("deprecation")
public static long getBlockSize(StatFs statFs) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return statFs.getBlockSizeLong();
} else {
return statFs.getBlockSize();
}
}
/**
* 获得SD卡总空间字节数 -1为SD卡不可用
*
* @return
*/
public static long getSDcardTotalSpace() {
String sdPath = getSDcardPath();
if (sdPath != null) {
StatFs fs = new StatFs(sdPath);
return fs.getBlockSize() * fs.getBlockCount();
} else {
return -1L;
}
}
/**
* Given a path return the number of free KB
*
* @param path to the file system
* @return free space in KB
*/
private static long freeSpaceCalculation(String path) {
StatFs stat = new StatFs(path);
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return availableBlocks * blockSize / 1024;
}
public static long getAvailableSpace(File dir) {
try {
final StatFs stats = new StatFs(dir.getPath());
return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return -1;
}
}
public static long getAvailableSpaceByPath(String path) {
StatFs statFs;
Exception e;
if (TextUtils.isEmpty(path)) {
return -1;
}
File dir = new File(path);
if (!dir.exists()) {
dir.mkdir();
}
long block = 0;
long size = 0;
try {
StatFs statFs2 = new StatFs(path);
try {
block = (long) statFs2.getAvailableBlocks();
size = (long) statFs2.getBlockSize();
statFs = statFs2;
} catch (Exception e2) {
e = e2;
statFs = statFs2;
e.printStackTrace();
return size * block;
}
} catch (Exception e3) {
e = e3;
e.printStackTrace();
return size * block;
}
return size * block;
}
@SuppressWarnings("deprecation")
private void init() {
try {
StatFs statFs = new StatFs(sdPath);
long totalBlocks = statFs.getBlockCount();// 区域块数
long availableBlocks = statFs.getAvailableBlocks();// 可利用区域块数
long blockSize = statFs.getBlockSize();// 每个区域块大小
nSDTotalSize = totalBlocks * blockSize;
nSDFreeSize = availableBlocks * blockSize;
} catch (Exception e) {
}
}
public static long getAvailableStorage() {
try {
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().toString());
return ((long) stat.getAvailableBlocks()) * ((long) stat.getBlockSize());
} catch (RuntimeException e) {
return 0;
}
}
/**
* @Description 获取sdcard容量
* @return
*/
@SuppressWarnings({
"deprecation", "unused"
})
private static long getSDAllSize() {
File path = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath());
long blockSize = sf.getBlockSize();
long allBlocks = sf.getBlockCount();
// 返回SD卡大小
// return allBlocks * blockSize; //单位Byte
// return (allBlocks * blockSize)/1024; //单位KB
return (allBlocks * blockSize) / 1024 / 1024; // 单位MB
}
private String getRomAvailableSize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(this, blockSize * availableBlocks);
}
/**
* 计算sdcard上的剩余空间.(单位:bytes)
*/
public static long freeSpaceOnSD() {
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long freeSize;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
freeSize = stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
} else {
//noinspection deprecation
freeSize = stat.getAvailableBlocks() * stat.getBlockSize();
}
return freeSize;
}
public static long getAvailableSpace(File dir) {
try {
final StatFs stats = new StatFs(dir.getPath());
return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
return -1;
}
}
/**
* Calculates and returns the amount of used disk space in bytes at the specified path.
*
* @param path Filesystem path at which to make the space calculations
* @return Amount of disk space used, in bytes
*/
@SuppressWarnings("deprecation")
public static long calculateUsedDiskSpaceInBytes(String path) {
final StatFs statFs = new StatFs(path);
final long blockSizeBytes = statFs.getBlockSize();
final long totalSpaceBytes = blockSizeBytes * statFs.getBlockCount();
final long availableSpaceBytes = blockSizeBytes * statFs.getAvailableBlocks();
return totalSpaceBytes - availableSpaceBytes;
}
@SuppressWarnings("deprecation")
private int getBlockSizeDep(final @NotNull StatFs stat) {
return stat.getBlockSize();
}
@SuppressWarnings("deprecation")
private static long getSize(StatFs statFs) {
int blockSize = statFs.getBlockSize();
int availableBlocks = statFs.getAvailableBlocks();
return (blockSize * availableBlocks) / (1024 * 1024);
}