/** * Checks if there is enough Space on SDCard * * @param updateSize size to Check (long) * @return <code>true</code> if the Update will fit on SDCard, <code>false</code> if not enough * space on SDCard. Will also return <code>false</code>, if the SDCard is not mounted as * read/write */ @SuppressWarnings("deprecation") public boolean hasEnoughSpaceOnSdCard(long updateSize) { RootTools.log("Checking SDcard size and that it is mounted as RW"); String status = Environment.getExternalStorageState(); if (!status.equals(Environment.MEDIA_MOUNTED)) { return false; } File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = 0; long availableBlocks = 0; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) { blockSize = stat.getBlockSize(); availableBlocks = stat.getAvailableBlocks(); } else { blockSize = stat.getBlockSizeLong(); availableBlocks = stat.getAvailableBlocksLong(); } return (updateSize < availableBlocks * blockSize); }
@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; }
@TargetApi(JELLY_BEAN_MR2) 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); }
/** * 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; }
private static long getStatFsSize(StatFs statFs, String blockSizeMethod, String availableBlocksMethod) { try { Method getBlockSizeMethod = statFs.getClass().getMethod(blockSizeMethod); getBlockSizeMethod.setAccessible(true); Method getAvailableBlocksMethod = statFs.getClass().getMethod(availableBlocksMethod); getAvailableBlocksMethod.setAccessible(true); long blockSize = (Long) getBlockSizeMethod.invoke(statFs); long availableBlocks = (Long) getAvailableBlocksMethod.invoke(statFs); return blockSize * availableBlocks; } catch (Throwable e) { OkLogger.printStackTrace(e); } return 0; }
/** * 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; }
/** * 获取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(); }
/** * 获得可用存储空间 * * @return 可用存储空间(单位b) */ public long getFreeSpace() { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize;//区块的大小 long totalBlocks;//区块总数 long availableBlocks;//可用区块的数量 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { blockSize = stat.getBlockSizeLong(); totalBlocks = stat.getBlockCountLong(); availableBlocks = stat.getAvailableBlocksLong(); } else { blockSize = stat.getBlockSize(); totalBlocks = stat.getBlockCount(); availableBlocks = stat.getAvailableBlocks(); } Log.e(TAG, "totalSpace:" + blockSize * totalBlocks + "...availableSpace:" + blockSize * availableBlocks); return blockSize * availableBlocks; }
/** * 获取SD卡可用剩余空间大小(还剩下多少空间) * * @return */ public static long getSDCardAvailableSize() { long availableBlockCount; long blockSize; if (isSDCardMounted()) { String dir = getSDCardBaseDir(); //StatFs是从C语言引过来的 StatFs statFs = new StatFs(dir); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { availableBlockCount = statFs.getAvailableBlocksLong();//有多少块 blockSize = statFs.getBlockSizeLong();//每块有多大 } else { availableBlockCount = statFs.getAvailableBlocks();//有多少块 blockSize = statFs.getBlockSize();//每块有多大 } return availableBlockCount * blockSize / 1024 / 1024; //总大小 } return 0; }
private static long getStatFsSize(StatFs statFs, String blockSizeMethod, String availableBlocksMethod) { try { Method getBlockSizeMethod = statFs.getClass().getMethod(blockSizeMethod); getBlockSizeMethod.setAccessible(true); Method getAvailableBlocksMethod = statFs.getClass().getMethod(availableBlocksMethod); getAvailableBlocksMethod.setAccessible(true); long blockSize = (Long) getBlockSizeMethod.invoke(statFs); long availableBlocks = (Long) getAvailableBlocksMethod.invoke(statFs); return blockSize * availableBlocks; } catch (Throwable e) { e.printStackTrace(); } return 0; }
/** * 判断存储空间大小是否满足条件 * * @param sizeByte * @return */ public static boolean isAvaiableSpace(float sizeByte) { boolean ishasSpace = false; if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { String sdcard = Environment.getExternalStorageDirectory().getPath(); StatFs statFs = new StatFs(sdcard); long blockSize = statFs.getBlockSize(); long blocks = statFs.getAvailableBlocks(); float availableSpare = blocks * blockSize; if (availableSpare > (sizeByte + 1024 * 1024)) { ishasSpace = true; } } return ishasSpace; }
/** * Return number of available bytes on the filesystem backing the given * {@link FileDescriptor}, minus any {@link #RESERVED_BYTES} buffer. */ private static long getAvailableBytes(FileDescriptor fd) throws IOException { try { //TODO only Sdcard check?? String sdcardDir = Environment.getExternalStorageDirectory().getPath(); StatFs stat = new StatFs(sdcardDir); long bytesAvailable = 0; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) { bytesAvailable = (long)stat.getBlockSizeLong() * (long)stat.getAvailableBlocksLong(); } else { bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks(); } return bytesAvailable - RESERVED_BYTES; } catch (Exception e) { throw new IOException("getAvailableBytes IOException"); } }
@Deprecated public static boolean checkRomSpaceEnough(long limitSize) { long allSize; long availableSize = 0; try { StatFs sf = new StatFs(Environment.getDataDirectory().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 false; } return true; }
private long getStorageAvailableSize(Context context) { try { statFS = new StatFs(storagePath); long storageAvailableBlocks = (long)statFS.getBlockSize() * (long)statFS.getAvailableBlocks(); statFS = null; return storageAvailableBlocks; } catch (Exception e) { Log.w("MonitoringManager", "getStorageAvailableSize : " + context.getString(R.string.log_monitoring_manager_error_get_storage_available_size) + " : " + e); databaseManager.insertLog(context, "" + context.getString(R.string.log_monitoring_manager_error_get_storage_available_size), new Date().getTime(), 2, false); return 0; } }
/** * 获取SD卡信息 * * @return SDCardInfo */ @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(); }
private long getStorageTotalSize(Context context) { try { statFS = new StatFs(storagePath); long storageTotalBlock = (long)statFS.getBlockSize() * (long)statFS.getBlockCount(); statFS = null; return storageTotalBlock; } catch (Exception e) { Log.w("MonitoringManager", "getStorageTotalSize : " + context.getString(R.string.log_monitoring_manager_error_get_storage_total_size) + " : " + e); databaseManager.insertLog(context, "" + context.getString(R.string.log_monitoring_manager_error_get_storage_total_size), new Date().getTime(), 2, false); return 0; } }
public static String t(Context context) { try { if (a(context, "android.permission.WRITE_EXTERNAL_STORAGE")) { String externalStorageState = Environment.getExternalStorageState(); if (externalStorageState == null || !externalStorageState.equals("mounted")) { return null; } externalStorageState = Environment.getExternalStorageDirectory().getPath(); if (externalStorageState == null) { return null; } StatFs statFs = new StatFs(externalStorageState); long blockCount = (((long) statFs.getBlockCount()) * ((long) statFs.getBlockSize ())) / 1000000; return String.valueOf((((long) statFs.getBlockSize()) * ((long) statFs .getAvailableBlocks())) / 1000000) + "/" + String.valueOf(blockCount); } f.warn("can not get the permission of android.permission.WRITE_EXTERNAL_STORAGE"); return null; } catch (Object th) { f.e(th); return null; } }
/** * 得到sd卡剩余大小 * * @return */ public static long getSDAvailableSize() { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = 0; long availableBlocks = 0; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) { blockSize = stat.getBlockSizeLong(); availableBlocks = stat.getAvailableBlocksLong(); } else { blockSize = stat.getBlockSize(); availableBlocks = stat.getAvailableBlocks(); } return blockSize * availableBlocks / 1024; }
/** * 判断当前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; }
/** * 获取SDka可用空间 * * @return */ private static long getSDcardAvailableSize() { if (checkSdCard()) { File path = Environment.getExternalStorageDirectory(); StatFs mStatFs = new StatFs(path.getPath()); long blockSizeLong = mStatFs.getBlockSizeLong(); long availableBlocksLong = mStatFs.getAvailableBlocksLong(); return blockSizeLong * availableBlocksLong; } else return 0; }
/** * 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; }
public String getTotalInternalMemorySize() { long j = 0; try { StatFs statFs = new StatFs(Environment.getDataDirectory().getPath()); j = ((long) statFs.getBlockCount()) * ((long) statFs.getBlockSize()); } catch (Exception e) { } return String.valueOf(j); }
/** * Check how much usable space is available at a given path. * * @param path The path to check * @return The space available in bytes */ @TargetApi(VERSION_CODES.GINGERBREAD) public static long getUsableSpace(File path) { if (Utils.hasGingerbread()) { return path.getUsableSpace(); } final StatFs stats = new StatFs(path.getPath()); return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks(); }
public static long getAvailableStorage() { try { StatFs stat = new StatFs(Environment.getExternalStorageDirectory().toString()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { return stat.getAvailableBlocksLong() * stat.getBlockSizeLong(); } else { return (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); } } catch (RuntimeException ex) { return 0; } }
/** * 获取空闲的空间大小 * @return 空间大小 */ public static long getFreeSpaceBytes() { long freeSpaceBytes; final StatFs statFs = new StatFs(getSDPath()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { freeSpaceBytes = statFs.getAvailableBytes(); } else { //noinspection deprecation freeSpaceBytes = statFs.getAvailableBlocks() * (long) statFs.getBlockSize(); } return freeSpaceBytes; }
/** * 获取空闲的空间大小 * @param path 文件路径 * @return 空间大小 */ public static long getFreeSpaceBytes(final String path) { long freeSpaceBytes; final StatFs statFs = new StatFs(path); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { freeSpaceBytes = statFs.getAvailableBytes(); } else { //noinspection deprecation freeSpaceBytes = statFs.getAvailableBlocks() * (long) statFs.getBlockSize(); } return freeSpaceBytes; }
/** * 获取手机内部剩余存储空间 * * @return */ public static long getAvailableInternalMemorySize() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return availableBlocks * blockSize; }
public static long getFreeSpaceBytes(final String path) { long freeSpaceBytes; final StatFs statFs = new StatFs(path); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { freeSpaceBytes = statFs.getAvailableBytes(); } else { //noinspection deprecation freeSpaceBytes = statFs.getAvailableBlocks() * (long) statFs.getBlockSize(); } return freeSpaceBytes; }
@Before public void setUp() { PowerMockito.mockStatic(Environment.class); PowerMockito.mockStatic(StatFsHelper.class); PowerMockito.mockStatic(SystemClock.class); mMockFileInternal = mock(File.class); mMockFileExternal = mock(File.class); mMockStatFsInternal = mock(StatFs.class); mMockStatFsExternal = mock(StatFs.class); PowerMockito.when(SystemClock.uptimeMillis()).thenReturn(System.currentTimeMillis()); }
private static long calculateDiskCacheSize(File dir) { long size = MIN_DISK_CACHE_SIZE; try { StatFs statFs = new StatFs(dir.getAbsolutePath()); long available = (long) statFs.getBlockCount() * statFs.getBlockSize(); // 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); }
/** * 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; }
@SuppressWarnings("deprecation") private static void refreshAvailableExternalStorage() { try { if (externalStorageExists()) { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); availableExternalStorageGB = (long)stat.getAvailableBlocks() * (long)stat.getBlockSize(); } availableExternalStorageGB = Utility.convertBytesToGB(availableExternalStorageGB); } catch (Exception e) { // Swallow } }
@SuppressWarnings("deprecation") private static void refreshTotalExternalStorage() { try { if (externalStorageExists()) { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); totalExternalStorageGB = (long)stat.getBlockCount() * (long)stat.getBlockSize(); } totalExternalStorageGB = Utility.convertBytesToGB(totalExternalStorageGB); } catch (Exception e) { // Swallow } }
/** * 获取SD卡剩余空间的大小 * * @return long SD卡剩余空间的大小(单位:byte) */ public static long getSDSize() { final String str = Environment.getExternalStorageDirectory().getPath(); final StatFs localStatFs = new StatFs(str); final long blockSize = localStatFs.getBlockSize(); return localStatFs.getAvailableBlocks() * blockSize; }
public static long getExternalStorageSize(Context context) { long j = 0; try { String[] paths = (String[]) StorageManager.class.getMethod("getVolumePaths", new Class[0]).invoke((StorageManager) context.getSystemService("storage"), new Object[0]); if (paths != null && paths.length >= 2) { StatFs statFs = new StatFs(paths[1]); j = ((((long) statFs.getBlockCount()) * ((long) statFs.getBlockSize())) / 1024) / 1024; } } catch (Exception e) { LogTool.e(TAG, "getExternalStorageSize. " + e.toString()); } return j; }
public static long getAvailableStorage() { String storageDirectory = null; storageDirectory = Environment.getExternalStorageDirectory().toString(); try { StatFs stat = new StatFs(storageDirectory); long avaliableSize = ((long) stat.getAvailableBlocks() * (long) stat.getBlockSize()); return avaliableSize; } catch (RuntimeException ex) { return 0; } }
public static long getUsableSpace(File path) { if (path == null) { return -1; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { return path.getUsableSpace()/1024/1024; } else { if (!path.exists()) { return 0; } else { final StatFs stats = new StatFs(path.getPath()); return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks()/1024/1024; } } }