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

The following examples show how to use android.os.StatFs#getBlockCountLong() . 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: 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 2
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 3
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 4
Source File: StorageUtil.java    From FileManager with Apache License 2.0 6 votes vote down vote up
/**
 * 获得手机sdcard的总空间大小(单位KB)
 */
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Deprecated
public static long getSDCardTotalSize() {

    if (!isSDCardAvailable()) {
        return 1;
    }

    File path = Environment.getExternalStorageDirectory();
    StatFs sf = new StatFs(path.getPath());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        return sf.getBlockSizeLong() * sf.getBlockCountLong();
    } else {
        // (jira ZB-43)计算数值如果是int类型的话,计算结果也会先当成int类型处理,可能造成结果溢出变为负数
        return (long) sf.getBlockSize() * sf.getBlockCount();
    }
}
 
Example 5
Source File: Cache.java    From external-resources with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation") public static long calculateDiskCacheSize(File dir) {
  long size = MIN_DISK_CACHE_SIZE;

  try {
    StatFs statFs = new StatFs(dir.getAbsolutePath());
    final long available;

    if (SDK_INT < JELLY_BEAN_MR2) {
      available = ((long) statFs.getBlockCount()) * statFs.getBlockSize();
    } else {
      available = statFs.getBlockCountLong() * statFs.getBlockSizeLong();
    }

    size = available / 50;
  } catch (IllegalArgumentException ignored) {
  }

  return Math.max(Math.min(size, MAX_DISK_CACHE_SIZE), MIN_DISK_CACHE_SIZE);
}
 
Example 6
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 7
Source File: CommonUtil.java    From HeroVideo-master with Apache License 2.0 6 votes vote down vote up
/**
 * 获取手机SD卡总空间
 *
 * @return
 */
private static long getSDcardTotalSize()
{

    if (checkSdCard())
    {
        File path = Environment.getExternalStorageDirectory();
        StatFs mStatFs = new StatFs(path.getPath());
        long blockSizeLong = mStatFs.getBlockSizeLong();
        long blockCountLong = mStatFs.getBlockCountLong();

        return blockSizeLong * blockCountLong;
    } else
    {
        return 0;
    }
}
 
Example 8
Source File: DiskUtil.java    From lrkFM with MIT License 5 votes vote down vote up
/**
 * 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.
 */
public static int busySpaceMebi(boolean external)
{
    StatFs statFs = getStats(external);
    long total = (statFs.getBlockCountLong() * statFs.getBlockSizeLong());
    long free  = (statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong());
    long used = (total - free) / MEBIBYTE;
    Log.d(TAG, "used disk space: " + String.valueOf(used/ MEBIBYTE));
    return (int) used;
}
 
Example 9
Source File: CommonUtil.java    From HeroVideo-master with Apache License 2.0 5 votes vote down vote up
/**
 * 获取手机内部存储总空间
 *
 * @return
 */
public static long getPhoneTotalSize()
{

    if (!checkSdCard())
    {
        File path = Environment.getDataDirectory();
        StatFs mStatFs = new StatFs(path.getPath());
        long blockSizeLong = mStatFs.getBlockSizeLong();
        long blockCountLong = mStatFs.getBlockCountLong();
        return blockSizeLong * blockCountLong;
    } else
        return getSDcardTotalSize();
}
 
Example 10
Source File: OkHttpUtils.java    From cathode with Apache License 2.0 5 votes vote down vote up
public static long getCacheSize(File dir) {
  long size = CACHE_SIZE_MIN;

  try {
    StatFs statFs = new StatFs(dir.getAbsolutePath());
    long available;
    available = statFs.getBlockCountLong() * statFs.getBlockSizeLong();

    size = available / 50;
  } catch (IllegalArgumentException e) {
    // Ignore
  }

  return Math.max(Math.min(size, CACHE_SIZE_MAX), CACHE_SIZE_MIN);
}
 
Example 11
Source File: RLAPICompat.java    From Roid-Library with Apache License 2.0 5 votes vote down vote up
public static long getBlockCount() {
    long size = 0;
    if (Environment.getExternalStorageDirectory() != null) {
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
        size = stat.getBlockCountLong();
    }
    return size;
}
 
Example 12
Source File: ApiCompatibilityUtils.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * See {@link android.os.StatFs#getBlockCount}.
 */
@SuppressWarnings("deprecation")
public static long getBlockCount(StatFs statFs) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return statFs.getBlockCountLong();
    } else {
        return statFs.getBlockCount();
    }
}
 
Example 13
Source File: DiskStat.java    From FileManager with Apache License 2.0 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 14
Source File: OkDownloadManager.java    From BlueBoard with Apache License 2.0 5 votes vote down vote up
public static long getTotalInternalMemorySize() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSizeLong();
    long totalBlocks = stat.getBlockCountLong();
    return totalBlocks * blockSize;
}
 
Example 15
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 16
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 17
Source File: FileUtils.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
/**
 * Return the total size of file system.
 *
 * @param anyPathInFs Any path in file system.
 * @return the total size of file system
 */
public static long getFsTotalSize(String anyPathInFs) {
    if (TextUtils.isEmpty(anyPathInFs)) return 0;
    StatFs statFs = new StatFs(anyPathInFs);
    long blockSize;
    long totalSize;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        blockSize = statFs.getBlockSizeLong();
        totalSize = statFs.getBlockCountLong();
    } else {
        blockSize = statFs.getBlockSize();
        totalSize = statFs.getBlockCount();
    }
    return blockSize * totalSize;
}
 
Example 18
Source File: OkDownloadManager.java    From BlueBoard 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.getBlockSizeLong();
        long totalBlocks = stat.getBlockCountLong();
        return totalBlocks * blockSize;
    } else {
        return -1;
    }
}
 
Example 19
Source File: MedicAndroidJavascript.java    From medic-android with GNU Affero General Public License v3.0 4 votes vote down vote up
@org.xwalk.core.JavascriptInterface
@android.webkit.JavascriptInterface
public String getDeviceInfo() {
	try {
		if (activityManager == null) {
			return jsonError("ActivityManager not set. Cannot retrieve RAM info.");
		}

		if (connectivityManager == null) {
			return jsonError("ConnectivityManager not set. Cannot retrieve network info.");
		}

		String versionName = parent.getPackageManager()
				.getPackageInfo(parent.getPackageName(), 0)
				.versionName;
		JSONObject appObject = new JSONObject();
		appObject.put("version", versionName);

		String androidVersion = Build.VERSION.RELEASE;
		int osApiLevel = Build.VERSION.SDK_INT;
		String osVersion = System.getProperty("os.version") + "(" + android.os.Build.VERSION.INCREMENTAL + ")";
		JSONObject softwareObject = new JSONObject();
		softwareObject
				.put("androidVersion", androidVersion)
				.put("osApiLevel", osApiLevel)
				.put("osVersion", osVersion);

		String device = Build.DEVICE;
		String model = Build.MODEL;
		String manufacturer = Build.BRAND;
		String hardware = Build.HARDWARE;
		Map<String, String> cpuInfo = getCPUInfo();
		JSONObject hardwareObject = new JSONObject();
		hardwareObject
				.put("device", device)
				.put("model", model)
				.put("manufacturer", manufacturer)
				.put("hardware", hardware)
				.put("cpuInfo", new JSONObject(cpuInfo));

		File dataDirectory = Environment.getDataDirectory();
		StatFs dataDirectoryStat = new StatFs(dataDirectory.getPath());
		long dataDirectoryBlockSize = dataDirectoryStat.getBlockSizeLong();
		long dataDirectoryAvailableBlocks = dataDirectoryStat.getAvailableBlocksLong();
		long dataDirectoryTotalBlocks = dataDirectoryStat.getBlockCountLong();
		long freeMemorySize = dataDirectoryAvailableBlocks * dataDirectoryBlockSize;
		long totalMemorySize = dataDirectoryTotalBlocks * dataDirectoryBlockSize;
		JSONObject storageObject = new JSONObject();
		storageObject
				.put("free", freeMemorySize)
				.put("total", totalMemorySize);

		MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
		activityManager.getMemoryInfo(memoryInfo);
		long totalRAMSize = memoryInfo.totalMem;
		long freeRAMSize = memoryInfo.availMem;
		long thresholdRAM = memoryInfo.threshold;
		JSONObject ramObject = new JSONObject();
		ramObject
				.put("free", freeRAMSize)
				.put("total", totalRAMSize)
				.put("threshold", thresholdRAM);

		NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
		JSONObject networkObject = new JSONObject();
		if (netInfo != null && Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
			NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
			int downSpeed = networkCapabilities.getLinkDownstreamBandwidthKbps();
			int upSpeed = networkCapabilities.getLinkUpstreamBandwidthKbps();
			networkObject
					.put("downSpeed", downSpeed)
					.put("upSpeed", upSpeed);
		}

		return new JSONObject()
				.put("app", appObject)
				.put("software", softwareObject)
				.put("hardware", hardwareObject)
				.put("storage", storageObject)
				.put("ram", ramObject)
				.put("network", networkObject)
				.toString();
	} catch(Exception ex) {
		return jsonError("Problem fetching device info: ", ex);
	}
}
 
Example 20
Source File: Device.java    From OsmGo with MIT License 4 votes vote down vote up
private long getDiskTotal() {
  StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
  return statFs.getBlockCountLong() * statFs.getBlockSizeLong();
}