Java Code Examples for android.os.StatFs#getTotalBytes()

The following examples show how to use android.os.StatFs#getTotalBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: APDE.java    From APDE with GNU General Public License v2.0 6 votes vote down vote up
public static String getAvailableSpace(File drive) {
	try {
		StatFs stat = new StatFs(drive.getAbsolutePath());
		
		DecimalFormat df = new DecimalFormat("#.00");
		df.setRoundingMode(RoundingMode.HALF_UP);
		
		long available;
		long total;
		
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
			available = stat.getAvailableBytes();
			total = stat.getTotalBytes();
		} else {
			available = stat.getAvailableBlocks() * stat.getBlockSize();
			total = stat.getBlockCount() * stat.getBlockSize();
		}
		
		return df.format(available / BYTE_PER_GB) + " GB free of " + df.format(total / BYTE_PER_GB) + " GB";
	} catch (IllegalArgumentException e) {
		return "Failed to stat FS";
	}
}
 
Example 2
Source File: HttpApiBase.java    From iview-android-tv with MIT License 6 votes vote down vote up
@SuppressWarnings("deprecation")
private static long calculateAvailableCacheSize(File dir) {
    long size = 0;
    try {
        StatFs statFs = new StatFs(dir.getAbsolutePath());
        int sdkInt = Build.VERSION.SDK_INT;
        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();
        }
        size = (long) Math.min(availableBytes * MAX_AVAILABLE_SPACE_USE_FRACTION, totalBytes * MAX_TOTAL_SPACE_USE_FRACTION);
    } catch (IllegalArgumentException ignored) {
        // ignored
    }
    return size;
}
 
Example 3
Source File: OBAnalyticsManagerCommunity.java    From GLEXP-Team-onebillion with Apache License 2.0 6 votes vote down vote up
@Override
public void deviceStorageUse ()
{
    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
    long bytesAvailable = stat.getAvailableBytes();
    long bytesTotal = stat.getTotalBytes();
    //
    long megaBytesAvailable = bytesAvailable / (1024 * 1024);
    long megaBytesTotal = bytesTotal / (1024 * 1024);
    long megaBytesUsed = megaBytesTotal - megaBytesAvailable;
    //
    deviceStatusValues.put(OBAnalytics.Params.DEVICE_USED_STORAGE, Long.valueOf(megaBytesUsed));
    deviceStatusValues.put(OBAnalytics.Params.DEVICE_TOTAL_STORAGE, Long.valueOf(megaBytesTotal));
    /*
     * Value is now stored in a buffer for regular updates to the database
     *
    Map<String, Object> parameters = new HashMap();
    parameters.put(OBAnalytics.Params.DEVICE_USED_STORAGE, Long.valueOf(megaBytesUsed));
    parameters.put(OBAnalytics.Params.DEVICE_TOTAL_STORAGE, Long.valueOf(megaBytesTotal));
    //
    logEvent(OBAnalytics.Event.DEVICE, parameters);
    */
}
 
Example 4
Source File: OBAnalyticsManagerOnline.java    From GLEXP-Team-onebillion with Apache License 2.0 6 votes vote down vote up
@Override
public void deviceStorageUse ()
{
    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
    long bytesAvailable = stat.getAvailableBytes();
    long bytesTotal = stat.getTotalBytes();
    //
    long megaBytesAvailable = bytesAvailable / (1024 * 1024);
    long megaBytesTotal = bytesTotal / (1024 * 1024);
    long megaBytesUsed = megaBytesTotal - megaBytesAvailable;
    //
    Map<String, Object> parameters = new HashMap();
    parameters.put(OBAnalytics.Params.DEVICE_USED_STORAGE, Long.valueOf(megaBytesUsed));
    parameters.put(OBAnalytics.Params.DEVICE_TOTAL_STORAGE, Long.valueOf(megaBytesTotal));
    //
    logEvent(OBAnalytics.Event.DEVICE, parameters);
}
 
Example 5
Source File: SDCardUtils.java    From XKnife-Android with Apache License 2.0 6 votes vote down vote up
/**
 * 获取SD卡信息
 *
 * @return SDCardInfo sd card info
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static String getSDCardInfo() {
    SDCardInfo sd = new SDCardInfo();
    if (!isSDCardEnable()) return "sdcard unable!";
    sd.isExist = true;
    StatFs sf = new StatFs(Environment.getExternalStorageDirectory().getPath());
    sd.totalBlocks = sf.getBlockCountLong();
    sd.blockByteSize = sf.getBlockSizeLong();
    sd.availableBlocks = sf.getAvailableBlocksLong();
    sd.availableBytes = sf.getAvailableBytes();
    sd.freeBlocks = sf.getFreeBlocksLong();
    sd.freeBytes = sf.getFreeBytes();
    sd.totalBytes = sf.getTotalBytes();
    return sd.toString();
}
 
Example 6
Source File: SDCardUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * 获取SD卡信息
 *
 * @return SDCardInfo
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static String getSDCardInfo() {
    if (!isSDCardEnable()) return null;
    SDCardInfo sd = new SDCardInfo();
    sd.isExist = true;
    StatFs sf = new StatFs(Environment.getExternalStorageDirectory().getPath());
    sd.totalBlocks = sf.getBlockCountLong();
    sd.blockByteSize = sf.getBlockSizeLong();
    sd.availableBlocks = sf.getAvailableBlocksLong();
    sd.availableBytes = sf.getAvailableBytes();
    sd.freeBlocks = sf.getFreeBlocksLong();
    sd.freeBytes = sf.getFreeBytes();
    sd.totalBytes = sf.getTotalBytes();
    return sd.toString();
}
 
Example 7
Source File: StorageActivity.java    From AndroidDemo with MIT License 6 votes vote down vote up
/**
 * 除去系统后的内存大小
 */
private void checkExceptSystemCapacity() {
    StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getPath());

    //存储块
    long blockCount = statFs.getBlockCount();
    //块大小
    long blockSize = statFs.getBlockSize();
    //可用块数量
    long availableCount = statFs.getAvailableBlocks();
    //剩余块数量,注:这个包含保留块(including reserved blocks)即应用无法使用的空间
    long freeBlocks = statFs.getFreeBlocks();

    long totalSize = statFs.getTotalBytes();
    long availableSize = statFs.getAvailableBytes();

    append("total = " + getUnit(totalSize));
    append("availableSize = " + getUnit(availableSize));
    append("=========");
    append("total = " + getUnit(blockSize * blockCount));
    append("available = " + getUnit(blockSize * availableCount));
    append("free = " + getUnit(blockSize * freeBlocks));
}
 
Example 8
Source File: ThetaObjectStorage.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
/**
 * Check Android Storage size.
 * @return Return a false if true, otherwise there is a minimum required value or more free
 */
public static boolean hasEnoughStorageSize() {
    StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
    float total = 1.0f;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        total = stat.getTotalBytes();
    } else {
        total = (float) stat.getBlockSize() * stat.getAvailableBlocks();
    }
    int v = (int) (total / (1024.f * 1024.f));
    if(BuildConfig.DEBUG) {
        if(v < LIMIT_APK_SIZE) {
            sLogger.warning("hasEnoughStorageSize is less than " + LIMIT_APK_SIZE + ", rest size =" + v);
        }
    }
    return v >= LIMIT_APK_SIZE;
}
 
Example 9
Source File: SDCardUtils.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 获取SD卡信息
 *
 * @return SDCardInfo
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static String getSDCardInfo() {
    if (!isSDCardEnable()) return null;
    SDCardInfo sd = new SDCardInfo();
    sd.isExist = true;
    StatFs sf = new StatFs(Environment.getExternalStorageDirectory().getPath());
    sd.totalBlocks = sf.getBlockCountLong();
    sd.blockByteSize = sf.getBlockSizeLong();
    sd.availableBlocks = sf.getAvailableBlocksLong();
    sd.availableBytes = sf.getAvailableBytes();
    sd.freeBlocks = sf.getFreeBlocksLong();
    sd.freeBytes = sf.getFreeBytes();
    sd.totalBytes = sf.getTotalBytes();
    return sd.toString();
}
 
Example 10
Source File: SDCardUtils.java    From zone-sdk with MIT License 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static SDCardInfo getSDCardInfo() {
    SDCardInfo sd = new SDCardInfo();
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        sd.isExist = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            File sdcardDir = Environment.getExternalStorageDirectory();
            StatFs sf = new StatFs(sdcardDir.getPath());

            sd.totalBlocks = sf.getBlockCountLong();
            sd.blockByteSize = sf.getBlockSizeLong();

            sd.availableBlocks = sf.getAvailableBlocksLong();
            sd.availableBytes = sf.getAvailableBytes();

            sd.freeBlocks = sf.getFreeBlocksLong();
            sd.freeBytes = sf.getFreeBytes();

            sd.totalBytes = sf.getTotalBytes();
        }
    }
    LogZSDK.INSTANCE.i(sd.toString());
    return sd;
}
 
Example 11
Source File: StorageUtils.java    From SprintNBA with Apache License 2.0 6 votes vote down vote up
/**
 * 获取SD卡信息
 *
 * @return
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static SDCardInfo getSDCardInfo() {
    SDCardInfo sd = new SDCardInfo();
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        sd.isExist = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            File sdcardDir = Environment.getExternalStorageDirectory();
            StatFs sf = new StatFs(sdcardDir.getPath());

            sd.totalBlocks = sf.getBlockCountLong();
            sd.blockByteSize = sf.getBlockSizeLong();

            sd.availableBlocks = sf.getAvailableBlocksLong();
            sd.availableBytes = sf.getAvailableBytes();

            sd.freeBlocks = sf.getFreeBlocksLong();
            sd.freeBytes = sf.getFreeBytes();

            sd.totalBytes = sf.getTotalBytes();
        }
    }
    LogUtils.i(TAG, sd.toString());
    return sd;
}
 
Example 12
Source File: SdCardUtil.java    From AndroidBase with Apache License 2.0 6 votes vote down vote up
/**
 * Get SD card info detail.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static SDCardInfo getSDCardInfo() {
    SDCardInfo sd = new SDCardInfo();
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        sd.isExist = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            File sdcardDir = Environment.getExternalStorageDirectory();
            StatFs sf = new StatFs(sdcardDir.getPath());

            sd.totalBlocks = sf.getBlockCountLong();
            sd.blockByteSize = sf.getBlockSizeLong();

            sd.availableBlocks = sf.getAvailableBlocksLong();
            sd.availableBytes = sf.getAvailableBytes();

            sd.freeBlocks = sf.getFreeBlocksLong();
            sd.freeBytes = sf.getFreeBytes();

            sd.totalBytes = sf.getTotalBytes();
        }
    }
    LogUtils.i( sd.toString());
    return sd;
}
 
Example 13
Source File: Storage.java    From batteryhub with Apache License 2.0 6 votes vote down vote up
/**
 * Returns free and total storage space in bytes
 *
 * @param path Path to the storage medium
 * @return Free and total space in long[]
 */
@Deprecated
private static long[] getStorageDetailsForPath(File path) {
    if (path == null) return new long[]{};
    final int KB = 1024;
    final int MB = KB * 1024;
    long free;
    long total;
    long blockSize;
    try {
        StatFs stats = new StatFs(path.getAbsolutePath());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            free = stats.getAvailableBytes() / MB;
            total = stats.getTotalBytes() / MB;
            return new long[]{free, total};
        } else {
            blockSize = (long) stats.getBlockSize();
            free = ((long) stats.getAvailableBlocks() * blockSize) / MB;
            total = ((long) stats.getBlockCount() * blockSize) / MB;
            if (free < 0 || total < 0) return new long[]{};
            return new long[]{free, total};
        }
    } catch (Exception e) {
        return new long[]{};
    }
}
 
Example 14
Source File: DeviceUtils.java    From MyUtil with Apache License 2.0 5 votes vote down vote up
/**
 * 获取手机内部总存储空间 单位byte
 * @return
 */
@SuppressWarnings("deprecation")
public static long getTotalInternalStorageSize() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());

    if(Build.VERSION.SDK_INT >= 18) {
        return stat.getTotalBytes();
    } else {
        return (long) stat.getBlockCount() * stat.getBlockSize();
    }
}
 
Example 15
Source File: SdCardUtil.java    From android-common with Apache License 2.0 5 votes vote down vote up
/**
 * Get SD card info detail.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static SDCardInfo getSDCardInfo() {
    SDCardInfo sd = new SDCardInfo();
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        sd.isExist = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            File sdcardDir = Environment.getExternalStorageDirectory();
            StatFs sf = new StatFs(sdcardDir.getPath());

            sd.totalBlocks = sf.getBlockCountLong();
            sd.blockByteSize = sf.getBlockSizeLong();

            sd.availableBlocks = sf.getAvailableBlocksLong();
            sd.availableBytes = sf.getAvailableBytes();

            sd.freeBlocks = sf.getFreeBlocksLong();
            sd.freeBytes = sf.getFreeBytes();

            sd.totalBytes = sf.getTotalBytes();
        }
    }
    if (Log.isPrint) {
        Log.i(TAG, sd.toString());
    }
    return sd;
}
 
Example 16
Source File: DeviceUtils.java    From MyUtil with Apache License 2.0 5 votes vote down vote up
/**
 * 获取SDCARD总的存储空间 单位byte
 * @return
 */
public static long getTotalExternalStorageSize() {
    if (isSdcardExisting()) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());

        if(Build.VERSION.SDK_INT >= 18) {
            return stat.getTotalBytes();
        } else {
            return (long) stat.getBlockCount() * stat.getBlockSize();
        }
    } else {
        return 0;
    }
}
 
Example 17
Source File: DiskUtils.java    From astrobee_android with Apache License 2.0 5 votes vote down vote up
public static long getMemorySizeFromStat(StatFs stat, int targetSpace) {
    if(stat == null) {
        return ERROR;
    }
    switch (targetSpace) {
        case GET_FREE_MEM:
            return stat.getFreeBytes();
        case GET_USED_MEM:
            return stat.getTotalBytes() - stat.getFreeBytes();
        case GET_TOTAL_MEM:
            return stat.getTotalBytes();
        default:
            return ERROR;
    }
}
 
Example 18
Source File: OtherGetRequestProcesser.java    From TVRemoteIME with GNU General Public License v2.0 5 votes vote down vote up
private NanoHTTPD.Response getSDCardStatResponse(){
    File path = Environment.getExternalStorageDirectory();
    StatFs stat = new StatFs(path.getPath());
    long totalBytes, availableBytes;
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2){
        totalBytes = (long)stat.getBlockCount() * stat.getBlockSize();
        availableBytes = (long)stat.getAvailableBlocks() * stat.getBlockSize();
    }else{
        totalBytes = stat.getTotalBytes();
        availableBytes = stat.getAvailableBytes();
    }
    return RemoteServer.createJSONResponse(NanoHTTPD.Response.Status.OK,
            "{\"totalBytes\":" + totalBytes + ", \"availableBytes\":" + availableBytes + "}");
}
 
Example 19
Source File: SdCardUtil.java    From LockDemo with Apache License 2.0 5 votes vote down vote up
/**
 * Get SD card info detail.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static SDCardInfo getSDCardInfo() {
    SDCardInfo sd = new SDCardInfo();
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        sd.isExist = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            File sdcardDir = Environment.getExternalStorageDirectory();
            StatFs sf = new StatFs(sdcardDir.getPath());

            sd.totalBlocks = sf.getBlockCountLong();
            sd.blockByteSize = sf.getBlockSizeLong();

            sd.availableBlocks = sf.getAvailableBlocksLong();
            sd.availableBytes = sf.getAvailableBytes();

            sd.freeBlocks = sf.getFreeBlocksLong();
            sd.freeBytes = sf.getFreeBytes();

            sd.totalBytes = sf.getTotalBytes();
        }
    }

        Log.i(TAG, sd.toString());

    return sd;
}
 
Example 20
Source File: Helper.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
static long getTotalStorageSpace() {
    StatFs stats = new StatFs(Environment.getDataDirectory().getAbsolutePath());
    return stats.getTotalBytes();
}