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

The following examples show how to use android.os.StatFs#getBlockSize() . 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: DownloaderService.java    From aedict with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Checks if given dictionary file exists. If not, user is prompted for a
 * download and the files are downloaded if requested.
 * @param activity context
 * @param downloader the downloader implementation
 * @param skipMissingMsg if true then the "dictionary is missing" message is shown only when there is not enough free space.
 * @return true if the files are available, false otherwise.
 */
private boolean checkDictionaryFile(final Activity activity, final AbstractDownloader downloader, final boolean skipMissingMsg) {
	if (!isComplete(downloader.targetDir)) {
		final StatFs stats = new StatFs("/sdcard");
		final long free = ((long) stats.getBlockSize()) * stats.getAvailableBlocks();
		final StringBuilder msg = new StringBuilder(AedictApp.format(R.string.dictionary_missing_download, downloader.dictName));
		if (free < downloader.expectedSize) {
			msg.append('\n');
			msg.append(AedictApp.format(R.string.warning_less_than_x_mb_free, downloader.expectedSize / 1024, free / 1024));
		}
		if (free >= downloader.expectedSize && skipMissingMsg) {
			download(downloader);
			activity.startActivity(new Intent(activity, DownloadActivity.class));
		} else {
			new DialogActivity.Builder(activity).setDialogListener(new DownloaderDialogActivity(downloader)).showYesNoDialog(msg.toString());
		}
		return false;
	}
	return true;
}
 
Example 2
Source File: StatFsHelper.java    From fresco with MIT License 6 votes vote down vote up
/**
 * Gets the information about the free storage space, including reserved blocks, 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 getFreeStorageSpace(StorageType storageType) {
  ensureInitialized();

  maybeUpdateStats();

  StatFs statFS = storageType == StorageType.INTERNAL ? mInternalStatFs : mExternalStatFs;
  if (statFS != null) {
    long blockSize, availableBlocks;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
      blockSize = statFS.getBlockSizeLong();
      availableBlocks = statFS.getFreeBlocksLong();
    } else {
      blockSize = statFS.getBlockSize();
      availableBlocks = statFS.getFreeBlocks();
    }
    return blockSize * availableBlocks;
  }
  return -1;
}
 
Example 3
Source File: TinkerUtils.java    From HotFixDemo with MIT License 6 votes vote down vote up
/**
 * 判断当前ROM空间是否足够大
 */
@Deprecated
public static boolean checkRomSpaceEnough(long limitSize) {
    long allSize;
    long availableSize = 0;
    try {
        File data = Environment.getDataDirectory();
        StatFs sf = new StatFs(data.getPath());
        availableSize = (long) sf.getAvailableBlocks() * (long) sf.getBlockSize();
        allSize = (long) sf.getBlockCount() * (long) sf.getBlockSize();
    } catch (Exception e) {
        allSize = 0;
    }

    if (allSize != 0 && availableSize > limitSize) {
        return true;
    }
    return false;
}
 
Example 4
Source File: FileUtil.java    From FriendBook with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 计算SD卡的剩余空间
 *
 * @return 返回-1,说明没有安装sd卡
 */
public static long getFreeDiskSpace() {
    String status = Environment.getExternalStorageState();
    long freeSpace = 0;
    if (status.equals(Environment.MEDIA_MOUNTED)) {
        try {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            freeSpace = availableBlocks * blockSize / 1024;
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        return -1;
    }
    return (freeSpace);
}
 
Example 5
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 6
Source File: StatFsHelper.java    From fresco with MIT License 6 votes vote down vote up
/**
 * Gets the information about the available storage space either internal or external depends on
 * the give input
 *
 * @param storageType Internal or external storage type
 * @return available space in bytes, 0 if no information is available
 */
@SuppressLint("DeprecatedMethod")
public long getAvailableStorageSpace(StorageType storageType) {
  ensureInitialized();

  maybeUpdateStats();

  StatFs statFS = storageType == StorageType.INTERNAL ? mInternalStatFs : mExternalStatFs;
  if (statFS != null) {
    long blockSize, availableBlocks;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
      blockSize = statFS.getBlockSizeLong();
      availableBlocks = statFS.getAvailableBlocksLong();
    } else {
      blockSize = statFS.getBlockSize();
      availableBlocks = statFS.getAvailableBlocks();
    }
    return blockSize * availableBlocks;
  }
  return 0;
}
 
Example 7
Source File: JarUtil.java    From letv with Apache License 2.0 5 votes vote down vote up
public static long getAvailableStorage() {
    try {
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory().toString());
        return ((long) stat.getAvailableBlocks()) * ((long) stat.getBlockSize());
    } catch (RuntimeException e) {
        return 0;
    }
}
 
Example 8
Source File: CommonUtils.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates and returns the amount of used disk space in bytes at the specified path.
 *
 * @param path Filesystem path at which to make the space calculations
 * @return Amount of disk space used, in bytes
 */
@SuppressWarnings("deprecation")
public static long calculateUsedDiskSpaceInBytes(String path) {
  final StatFs statFs = new StatFs(path);
  final long blockSizeBytes = statFs.getBlockSize();
  final long totalSpaceBytes = blockSizeBytes * statFs.getBlockCount();
  final long availableSpaceBytes = blockSizeBytes * statFs.getAvailableBlocks();
  return totalSpaceBytes - availableSpaceBytes;
}
 
Example 9
Source File: OtherUtils.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
public static long getAvailableSpace(File dir) {
    try {
        final StatFs stats = new StatFs(dir.getPath());
        return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();
    } catch (Throwable e) {
        LogUtils.e(e.getMessage(), e);
        return -1;
    }

}
 
Example 10
Source File: SDCardUtil.java    From BaseProject with MIT License 5 votes vote down vote up
/**
 * 计算sdcard上的剩余空间.(单位:bytes)
 */
public static long freeSpaceOnSD() {
    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
    long   freeSize;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        freeSize = stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
    } else {
        //noinspection deprecation
        freeSize = stat.getAvailableBlocks() * stat.getBlockSize();
    }
    return freeSize;
}
 
Example 11
Source File: api_o.java    From styT with Apache License 2.0 5 votes vote down vote up
private String getRomAvailableSize() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    return Formatter.formatFileSize(this, blockSize * availableBlocks);
}
 
Example 12
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 13
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 14
Source File: SDCardManager.java    From SimplifyReader 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: StoreUtils.java    From letv with Apache License 2.0 5 votes vote down vote up
public static long getAvailableSpaceByPath(String path) {
    StatFs statFs;
    Exception e;
    if (TextUtils.isEmpty(path)) {
        return -1;
    }
    File dir = new File(path);
    if (!dir.exists()) {
        dir.mkdir();
    }
    long block = 0;
    long size = 0;
    try {
        StatFs statFs2 = new StatFs(path);
        try {
            block = (long) statFs2.getAvailableBlocks();
            size = (long) statFs2.getBlockSize();
            statFs = statFs2;
        } catch (Exception e2) {
            e = e2;
            statFs = statFs2;
            e.printStackTrace();
            return size * block;
        }
    } catch (Exception e3) {
        e = e3;
        e.printStackTrace();
        return size * block;
    }
    return size * block;
}
 
Example 16
Source File: OtherUtils.java    From android-open-project-demo with Apache License 2.0 5 votes vote down vote up
public static long getAvailableSpace(File dir) {
    try {
        final StatFs stats = new StatFs(dir.getPath());
        return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();
    } catch (Throwable e) {
        LogUtils.e(e.getMessage(), e);
        return -1;
    }

}
 
Example 17
Source File: DirectoryManager.java    From reader with MIT License 5 votes vote down vote up
/**
 * Given a path return the number of free KB
 * 
 * @param path to the file system
 * @return free space in KB
 */
private static long freeSpaceCalculation(String path) {
    StatFs stat = new StatFs(path);
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    return availableBlocks * blockSize / 1024;
}
 
Example 18
Source File: FileHelper.java    From appcan-android with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 获得SD卡总空间字节数 -1为SD卡不可用
 *
 * @return
 */
public static long getSDcardTotalSpace() {
    String sdPath = getSDcardPath();
    if (sdPath != null) {
        StatFs fs = new StatFs(sdPath);
        return fs.getBlockSize() * fs.getBlockCount();
    } else {
        return -1L;
    }
}
 
Example 19
Source File: UpdateMenuItemHelper.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private static long getSize(StatFs statFs) {
    int blockSize = statFs.getBlockSize();
    int availableBlocks = statFs.getAvailableBlocks();
    return (blockSize * availableBlocks) / (1024 * 1024);
}
 
Example 20
Source File: DefaultAndroidEventProcessor.java    From sentry-android with MIT License 4 votes vote down vote up
@SuppressWarnings("deprecation")
private int getBlockSizeDep(final @NotNull StatFs stat) {
  return stat.getBlockSize();
}