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

The following examples show how to use android.os.StatFs#getBlockSizeLong() . 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: Storage.java    From android-storage with Apache License 2.0 6 votes vote down vote up
public long getUsedSpace(String dir, SizeUnit sizeUnit) {
    StatFs statFs = new StatFs(dir);
    long availableBlocks;
    long blockSize;
    long totalBlocks;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
        availableBlocks = statFs.getAvailableBlocks();
        blockSize = statFs.getBlockSize();
        totalBlocks = statFs.getBlockCount();
    } else {
        availableBlocks = statFs.getAvailableBlocksLong();
        blockSize = statFs.getBlockSizeLong();
        totalBlocks = statFs.getBlockCountLong();
    }
    long usedBytes = totalBlocks * blockSize - availableBlocks * blockSize;
    return usedBytes / sizeUnit.inBytes();
}
 
Example 2
Source File: DiskStat.java    From CleanExpert with MIT License 6 votes vote down vote up
private void calculateExternalSpace() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        File sdcardDir = Environment.getExternalStorageDirectory();
        StatFs sf = new StatFs(sdcardDir.getPath());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            mExternalBlockSize = sf.getBlockSizeLong();
            mExternalBlockCount = sf.getBlockCountLong();
            mExternalAvailableBlocks = sf.getAvailableBlocksLong();
        } else {
            mExternalBlockSize = sf.getBlockSize();
            mExternalBlockCount = sf.getBlockCount();
            mExternalAvailableBlocks = sf.getAvailableBlocks();
        }
    }
}
 
Example 3
Source File: SDCardUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 获取对应路径的空间总大小
 * @param path 路径
 * @return 对应路径的空间总大小
 */
public static long getAllBlockSize(final String path) {
    try {
        // 获取路径的存储空间信息
        StatFs statFs = new StatFs(path);
        // 单个数据块的大小、数据块数量
        long blockSize, blockCount;
        // 版本兼容
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            blockSize = statFs.getBlockSizeLong();
            blockCount = statFs.getBlockCountLong();
        } else {
            blockSize = statFs.getBlockSize();
            blockCount = statFs.getBlockCount();
        }
        // 返回空间总大小
        return (blockCount * blockSize);
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "getAllBlockSize");
    }
    return 0L;
}
 
Example 4
Source File: AndroidDeviceDetailsInfo.java    From applivery-android-sdk with Apache License 2.0 6 votes vote down vote up
@Override public String getFreeDiskPercentage() {
  File rootDirectory = Environment.getRootDirectory();
  StatFs fileSystemData = new StatFs(rootDirectory.getPath());

  long blockSize;
  long totalSize;
  long availableSize;
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    blockSize = fileSystemData.getBlockSizeLong();
    totalSize = fileSystemData.getBlockCountLong() * blockSize;
    availableSize = fileSystemData.getAvailableBlocksLong() * blockSize;
  } else {
    blockSize = fileSystemData.getBlockSize();
    totalSize = fileSystemData.getBlockCount() * blockSize;
    availableSize = fileSystemData.getAvailableBlocks() * blockSize;
  }

  long freeDiskPercentage = availableSize * 100 / totalSize;
  return String.valueOf(freeDiskPercentage);
}
 
Example 5
Source File: SDCardUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 获取对应路径已使用空间大小
 * @param path 路径
 * @return 对应路径已使用空间大小
 */
public static long getUsedBlocks(final String path) {
    try {
        // 获取路径的存储空间信息
        StatFs statFs = new StatFs(path);
        // 单个数据块的大小、数据块数量、空闲的数据块数量
        long blockSize, blockCount, availableBlocks;
        // 版本兼容
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            blockSize = statFs.getBlockSizeLong();
            blockCount = statFs.getBlockCountLong();
            availableBlocks = statFs.getAvailableBlocksLong();
        } else {
            blockSize = statFs.getBlockSize();
            blockCount = statFs.getBlockCount();
            availableBlocks = statFs.getAvailableBlocks();
        }
        // 返回已使用空间大小
        return ((blockCount - availableBlocks) * blockSize);
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "getUsedBlocks");
    }
    return 0L;
}
 
Example 6
Source File: StorageUtils.java    From Ezalor with Apache License 2.0 6 votes vote down vote up
@SuppressLint("NewApi")
public static long getTotalSizeByPath(String path) {
    StatFs stat = new StatFs(path);
    long blockCount = 0;
    long blockSize = 0;
    long totalSize = 0;
    try {
        if (Build.VERSION.SDK_INT >= 19) {
            blockCount = stat.getBlockCountLong();
            blockSize = stat.getBlockSizeLong();
        } else {

            blockCount = stat.getBlockCount();
            blockSize = stat.getBlockSize();
        }

    } catch (Exception e) {
        LogUtils.logeForce(e);
    }

    totalSize = blockCount * blockSize;
    return totalSize;
}
 
Example 7
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 8
Source File: DiskStat.java    From CleanExpert with MIT License 5 votes vote down vote up
private void calculateInternalSpace() {
    File root = Environment.getRootDirectory();
    StatFs sf = new StatFs(root.getPath());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        mInternalBlockSize = sf.getBlockSizeLong();
        mInternalBlockCount = sf.getBlockCountLong();
        mInternalAvailableBlocks = sf.getAvailableBlocksLong();
    } else {
        mInternalBlockSize = sf.getBlockSize();
        mInternalBlockCount = sf.getBlockCount();
        mInternalAvailableBlocks = sf.getAvailableBlocks();
    }
}
 
Example 9
Source File: DiskUtil.java    From lrkFM with MIT License 5 votes vote down vote up
/**
 * 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.
 */
public static int totalSpaceGibi(boolean external)
{
    StatFs statFs = getStats(external);
    long total = (statFs.getBlockCountLong() * statFs.getBlockSizeLong()) / GIBIBYTE;
    Log.d(TAG, "Total disk space: " + String.valueOf(total));
    return (int) total;
}
 
Example 10
Source File: MizLib.java    From Mizuu with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public static long getFreeMemory() {
    StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
    if (hasJellyBeanMR2())
        return stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
    else
        return stat.getAvailableBlocks() * stat.getBlockSize();
}
 
Example 11
Source File: DownloadRecordActivity.java    From Dainty with Apache License 2.0 5 votes vote down vote up
private void refreshStorageStatus() {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getPath());
        long blockSize = statFs.getBlockSizeLong();
        long totalBlocks = statFs.getBlockCountLong();
        long availableBlocks = statFs.getAvailableBlocksLong();
        String totalSize = Formatter.formatFileSize(this, blockSize * totalBlocks);
        String availableSize = Formatter.formatFileSize(this, blockSize * availableBlocks);
        textProgressBar.setTextAndProgress("内置存储可用:" + availableSize + "/共:" + totalSize, (int) ((float) availableBlocks / totalBlocks * 100));
    } else {
        textProgressBar.setTextAndProgress("内置存储不可用", 0);
    }
}
 
Example 12
Source File: CommonUtil.java    From HeroVideo-master with Apache License 2.0 5 votes vote down vote up
/**
 * 获取手机内存存储可用空间
 *
 * @return
 */
public static long getPhoneAvailableSize()
{

    if (!checkSdCard())
    {
        File path = Environment.getDataDirectory();
        StatFs mStatFs = new StatFs(path.getPath());
        long blockSizeLong = mStatFs.getBlockSizeLong();
        long availableBlocksLong = mStatFs.getAvailableBlocksLong();
        return blockSizeLong * availableBlocksLong;
    } else
        return getSDcardAvailableSize();
}
 
Example 13
Source File: DefaultAndroidEventProcessor.java    From sentry-android with MIT License 5 votes vote down vote up
@SuppressWarnings("ObsoleteSdkInt")
private long getBlockSizeLong(final @NotNull StatFs stat) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    return stat.getBlockSizeLong();
  }
  return getBlockSizeDep(stat);
}
 
Example 14
Source File: ApiCompatibilityUtils.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 15
Source File: SDCardUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 返回对应路径的空间大小信息
 * @param path 路径
 * @return long[], 0 = 总空间大小, 1 = 空闲空间大小, 2 = 已使用空间大小
 */
public static long[] getBlockSizeInfos(final String path) {
    try {
        // 获取路径的存储空间信息
        StatFs statFs = new StatFs(path);
        // 单个数据块的大小、数据块数量、空闲的数据块数量
        long blockSize, blockCount, availableBlocks;
        // 版本兼容
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            blockSize = statFs.getBlockSizeLong();
            blockCount = statFs.getBlockCountLong();
            availableBlocks = statFs.getAvailableBlocksLong();
        } else {
            blockSize = statFs.getBlockSize();
            blockCount = statFs.getBlockCount();
            availableBlocks = statFs.getAvailableBlocks();
        }
        // 计算空间信息
        long[] blocks = new long[3];
        blocks[0] = blockSize * blockCount;
        blocks[1] = blockSize * availableBlocks;
        blocks[2] = ((blockCount - availableBlocks) * blockSize);
        // 返回空间信息
        return blocks;
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "getBlockSizeInfos");
    }
    return null;
}
 
Example 16
Source File: SdCardUtil.java    From android-common with Apache License 2.0 5 votes vote down vote up
/**
 * Get available size of SD card.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static long getAvailableSize(String path) {
    try {
        File base = new File(path);
        StatFs stat = new StatFs(base.getPath());
        return stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}
 
Example 17
Source File: StorageConstraintPlugin.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
public static long getAvailableInternalMemorySize() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSizeLong();
    long blocksAvailable = stat.getAvailableBlocksLong();
    int size = 1048576; // blocksize of 1 Mb
    return ((blocksAvailable * blockSize) / size);
}
 
Example 18
Source File: SDUtils.java    From YCAudioPlayer with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static long getAvailableSize() {
    if (isMounted()) {
        StatFs stat = new StatFs(getSDCardPath());
        // 获得可用的块的数量
        long count = stat.getAvailableBlocksLong();
        long size = stat.getBlockSizeLong();
        return count * size;
    }
    return 0;
}
 
Example 19
Source File: EasyMemoryMod.java    From easydeviceinfo with Apache License 2.0 5 votes vote down vote up
/**
 * Gets available internal memory size.
 *
 * @return the available internal memory size
 */
public final long getAvailableInternalMemorySize() {
  File path = getDataDirectory();
  StatFs stat = new StatFs(path.getPath());
  long blockSize;
  long availableBlocks;
  if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) {
    blockSize = stat.getBlockSizeLong();
    availableBlocks = stat.getAvailableBlocksLong();
  } else {
    blockSize = stat.getBlockSize();
    availableBlocks = stat.getAvailableBlocks();
  }
  return availableBlocks * blockSize;
}
 
Example 20
Source File: ChatLogStorageManager.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
private long getBlockSize() {
    if (mBlockSize == 0L) {
        File chatLogDir = mServerConfigManager.getChatLogDir();
        if (!chatLogDir.exists())
            return 0L;
        StatFs statFs = new StatFs(chatLogDir.getAbsolutePath());
        if (Build.VERSION.SDK_INT >= 18)
            mBlockSize = statFs.getBlockSizeLong();
        else
            mBlockSize = statFs.getBlockSize();
    }
    return mBlockSize;
}