-
-
Save markus2610/5e7ccf0d34eac4104293 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.tom.utils; | |
| import android.annotation.SuppressLint; | |
| import android.os.Build; | |
| import android.os.Environment; | |
| import android.os.StatFs; | |
| /** | |
| * Created by Tom on 7/15/13. | |
| * Some helper methods for FS queries. | |
| */ | |
| public class DiskUtils { | |
| private static final long MEGA_BYTE = 1048576; | |
| static int sdkInt = Build.VERSION.SDK_INT; | |
| /** | |
| * Calculates total space on disk | |
| * | |
| * @param external If true will query external disk, otherwise will query internal disk. | |
| * @return Number of mega bytes on disk. | |
| */ | |
| @SuppressLint("NewApi") | |
| @SuppressWarnings("deprecation") | |
| public static int totalSpace(boolean external) { | |
| StatFs statFs = getStats(external); | |
| long totalBytes; | |
| if (sdkInt < Build.VERSION_CODES.JELLY_BEAN_MR2) { | |
| int blockSize = statFs.getBlockSize(); | |
| totalBytes = (((long) statFs.getBlockCount()) * blockSize) / MEGA_BYTE; | |
| } else { | |
| totalBytes = statFs.getTotalBytes(); | |
| } | |
| return (int) totalBytes; | |
| } | |
| /** | |
| * Calculates free space on disk | |
| * | |
| * @param external If true will query external disk, otherwise will query internal disk. | |
| * @return Number of free mega bytes on disk. | |
| */ | |
| @SuppressLint("NewApi") | |
| @SuppressWarnings("deprecation") | |
| public static int freeSpace(boolean external) { | |
| StatFs statFs = getStats(external); | |
| long totalBytes; | |
| long availableBytes; | |
| if (sdkInt < Build.VERSION_CODES.JELLY_BEAN_MR2) { | |
| int blockSize = statFs.getBlockSize(); | |
| availableBytes = ((long) statFs.getAvailableBlocks()) * blockSize; | |
| } else { | |
| availableBytes = statFs.getAvailableBytes(); | |
| } | |
| return (int) (availableBytes / MEGA_BYTE); | |
| } | |
| /** | |
| * Calculates occupied space on disk | |
| * | |
| * @param external If true will query external disk, otherwise will query internal disk. | |
| * @return Number of occupied mega bytes on disk. | |
| */ | |
| @SuppressLint("NewApi") | |
| @SuppressWarnings("deprecation") | |
| public static int busySpace(boolean external) { | |
| StatFs statFs = getStats(external); | |
| long totalBytes; | |
| long availableBytes; | |
| if (sdkInt < Build.VERSION_CODES.JELLY_BEAN_MR2) { | |
| int blockSize = statFs.getBlockSize(); | |
| availableBytes = ((long) statFs.getAvailableBlocks()) * blockSize; | |
| totalBytes = ((long) statFs.getBlockCount()) * blockSize; | |
| } else { | |
| availableBytes = statFs.getAvailableBytes(); | |
| totalBytes = statFs.getTotalBytes(); | |
| } | |
| return (int) ((totalBytes - availableBytes) / MEGA_BYTE); | |
| } | |
| private static StatFs getStats(boolean external) { | |
| String path; | |
| if (external) { | |
| path = Environment.getExternalStorageDirectory().getAbsolutePath(); | |
| } else { | |
| path = Environment.getRootDirectory().getAbsolutePath(); | |
| } | |
| return new StatFs(path); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment