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

The following examples show how to use android.os.StatFs#getBlockCount() . 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: FileUtil.java    From android-file-chooser with Apache License 2.0 6 votes vote down vote up
public static long readSDCard(Context context, Boolean isRemovable, Boolean freeOrTotal) {
    DecimalFormat df = new DecimalFormat("0.00");
    getStoragePath(context, isRemovable);
    StatFs sf = new StatFs(getStoragePath(context, isRemovable));
    long blockSize;
    long blockCount;
    long availCount;
    if (Build.VERSION.SDK_INT > 18) {
        blockSize = sf.getBlockSizeLong(); //文件存储时每一个存储块的大小为4KB
        blockCount = sf.getBlockCountLong();//存储区域的存储块的总个数
        availCount = sf.getFreeBlocksLong();//存储区域中可用的存储块的个数(剩余的存储大小)
    } else {
        blockSize = sf.getBlockSize();
        blockCount = sf.getBlockCount();
        availCount = sf.getFreeBlocks();
    }
    //Log.d("sss", "总的存储空间大小:" + blockSize * blockCount / 1073741824 + "GB" + ",剩余空间:"
    //    + availCount * blockSize / 1073741824 + "GB"
    //    + "--存储块的总个数--" + blockCount + "--一个存储块的大小--" + blockSize / 1024 + "KB");
    //return df.format((freeOrTotal ? availCount : blockCount) * blockSize / 1073741824.0);
    return (freeOrTotal ? availCount : blockCount) * blockSize;
    //return "-1";
}
 
Example 2
Source File: Utils.java    From picasso with Apache License 2.0 6 votes vote down vote up
static long calculateDiskCacheSize(File dir) {
  long size = MIN_DISK_CACHE_SIZE;

  try {
    StatFs statFs = new StatFs(dir.getAbsolutePath());
    //noinspection deprecation
    long blockCount =
        SDK_INT < JELLY_BEAN_MR2 ? (long) statFs.getBlockCount() : statFs.getBlockCountLong();
    //noinspection deprecation
    long blockSize =
        SDK_INT < JELLY_BEAN_MR2 ? (long) statFs.getBlockSize() : statFs.getBlockSizeLong();
    long available = blockCount * blockSize;
    // Target 2% of the total space.
    size = available / 50;
  } catch (IllegalArgumentException ignored) {
  }

  // Bound inside min/max size for disk cache.
  return Math.max(Math.min(size, MAX_DISK_CACHE_SIZE), MIN_DISK_CACHE_SIZE);
}
 
Example 3
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 4
Source File: StorageUtil.java    From mobile-manager-tool with MIT License 6 votes vote down vote up
public static long getTotalExternal_SDMemorySize() {
    if (isSDCardExist()) {
        File path = Environment.getExternalStorageDirectory();
        File externalSD = new File(path.getPath() + "/external_sd");
        if (externalSD.exists() && externalSD.isDirectory()) {
            StatFs stat = new StatFs(path.getPath() + "/external_sd");
            long blockSize = stat.getBlockSize();
            long totalBlocks = stat.getBlockCount();
            if (getTotalExternalMemorySize() != -1
                    && getTotalExternalMemorySize() != totalBlocks
                    * blockSize) {
                return totalBlocks * blockSize;
            } else {
                return ERROR;
            }
        } else {
            return ERROR;
        }

    } else {
        return ERROR;
    }
}
 
Example 5
Source File: CommonUtils.java    From HHComicViewer with Apache License 2.0 6 votes vote down vote up
public static String getStorageBlockSpace(String path) {
    try {
        //获取剩余存储空间
        File filePath = new File(path);
        StatFs sf = new StatFs(filePath.getPath());//创建StatFs对象
        long blockSize = sf.getBlockSize();//获得blockSize
        long totalBlock = sf.getBlockCount();//获得全部block
        long availableBlock = sf.getAvailableBlocks();//获取可用的block
        //用String数组来存放Block信息
        String[] total = fileSize(totalBlock * blockSize);
        String[] available = fileSize(availableBlock * blockSize);
        return "剩余空间:" + available[0] + available[1] + "/" + total[0] + total[1];
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}
 
Example 6
Source File: ResourceManager.java    From GreenDamFileExploere with Apache License 2.0 6 votes vote down vote up
public static void calcStorageSize() {
    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的总数
        
        mToalBytes = (long) (blockSize * totalBlocks) ;
        long availableBlocks = stat.getAvailableBlocks(); // 获取可用块大小
        
        
        mUsedBytes= (long) ((totalBlocks - availableBlocks) * blockSize);
        mFreeBytes = mToalBytes - mUsedBytes;
        
        System.out.println("存储空间" + TextUtil.getSizeSting(mToalBytes) + ",已用" +  TextUtil.getSizeSting(mUsedBytes) + "剩余:" + TextUtil.getSizeSting(mFreeBytes));
    } else {
        System.out.println("存储卡不存在:存储空间" + TextUtil.getSizeSting(mToalBytes) + ",已用" +  TextUtil.getSizeSting(mUsedBytes) + "剩余:" + TextUtil.getSizeSting(mFreeBytes));
    }
}
 
Example 7
Source File: DiskFileUtils.java    From cube-sdk with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static long getUsedSpace(File path) {
    if (path == null) {
        return -1;
    }

    if (Version.hasGingerbread()) {
        return path.getTotalSpace() - path.getUsableSpace();
    } else {
        if (!path.exists()) {
            return -1;
        } else {
            final StatFs stats = new StatFs(path.getPath());
            return (long) stats.getBlockSize() * (stats.getBlockCount() - stats.getAvailableBlocks());
        }
    }
}
 
Example 8
Source File: StatFsHelper.java    From fresco with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 9
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 10
Source File: ChatAttachAlertDocumentLayout.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private String getRootSubtitle(String path) {
    try {
        StatFs stat = new StatFs(path);
        long total = (long) stat.getBlockCount() * (long) stat.getBlockSize();
        long free = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
        if (total == 0) {
            return "";
        }
        return LocaleController.formatString("FreeOfTotal", R.string.FreeOfTotal, AndroidUtilities.formatFileSize(free), AndroidUtilities.formatFileSize(total));
    } catch (Exception e) {
        FileLog.e(e);
    }
    return path;
}
 
Example 11
Source File: StorageUtil.java    From TvLauncher with Apache License 2.0 5 votes vote down vote up
/**
 * 获取SDCARD总的存储空间
 *
 * @return
 */
public long getTotalExternalMemorySize() {
    if (externalMemoryAvailable()) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return totalBlocks * blockSize;
    } else {
        return ERROR;
    }
}
 
Example 12
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 13
Source File: FileSizeUtil.java    From styT with Apache License 2.0 5 votes vote down vote up
/**
 * 获取SDCARD总的存储空间
 *
 * @return
 */
public static long getTotalExternalMemorySize() {
    if (externalMemoryAvailable()) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return totalBlocks * blockSize;
    } else {
        return ERROR;
    }
}
 
Example 14
Source File: SDCardManager.java    From Dota2Helper with Apache License 2.0 5 votes vote down vote up
@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) {

	}
}
 
Example 15
Source File: CommonUtil.java    From sctalk with Apache License 2.0 5 votes vote down vote up
/**
 * @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
}
 
Example 16
Source File: Utility.java    From Kernel-Tuner with GNU General Public License v3.0 5 votes vote down vote up
public static long getTotalSpaceInBytesOnInternalStorage()
{
    long usedSpace;
    StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
    usedSpace = ((long) stat.getBlockCount()) * (long) stat.getBlockSize();

    return usedSpace;
}
 
Example 17
Source File: OkDownloadManager.java    From OkDownload with Apache License 2.0 5 votes vote down vote up
public static long getTotalExternalMemorySize() {
    if (hasSDCard()) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return totalBlocks * blockSize;
    } else {
        return -1;
    }
}
 
Example 18
Source File: Utils.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
public static long getImageCacheDirTotalMemory(Context context) {
    File statDir = getImageCacheDir(context);
    while (statDir != null && !statDir.exists()) {
        statDir = statDir.getParentFile();
    }
    if (statDir == null) {
        return 100 * 1024 * 1024; // just return a minimal amount
    }
    StatFs stat = new StatFs(statDir.getPath());
    if (Build.VERSION.SDK_INT < 18) {
        return (long) stat.getBlockCount() * (long) stat.getBlockSize();
    } else {
        return stat.getBlockCountLong() * stat.getBlockSizeLong();
    }
}
 
Example 19
Source File: MemoryInfoUtil.java    From letv with Apache License 2.0 4 votes vote down vote up
public static long getTotalInternalMemorySize() {
    StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
    return ((long) stat.getBlockCount()) * ((long) stat.getBlockSize());
}
 
Example 20
Source File: SystemModule.java    From COCOFramework with Apache License 2.0 4 votes vote down vote up
private int calculateDiskCacheSize(File dir) {
    StatFs statFs = new StatFs(dir.getAbsolutePath());
    int available = statFs.getBlockCount() * statFs.getBlockSize();
    int size = available / 50;
    return Math.max(Math.min(size, MAX_DISK_CACHE_SIZE), MIN_DISK_CACHE_SIZE);
}