android.os.StatFs Java Examples

The following examples show how to use android.os.StatFs. 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: EasyMemoryMod.java    From easydeviceinfo with Apache License 2.0 6 votes vote down vote up
/**
 * Gets total external memory size.
 *
 * @return the total external memory size
 */
public final long getTotalExternalMemorySize() {
  if (externalMemoryAvailable()) {
    File path = getExternalStorageDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize;
    long totalBlocks;
    if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) {
      blockSize = stat.getBlockSizeLong();
      totalBlocks = stat.getBlockCountLong();
    } else {
      blockSize = stat.getBlockSize();
      totalBlocks = stat.getBlockCount();
    }
    return totalBlocks * blockSize;
  } else {
    return 0;
  }
}
 
Example #2
Source File: Storage.java    From batteryhub with Apache License 2.0 6 votes vote down vote up
/**
 * Returns free and total storage space in bytes
 *
 * @param path Path to the storage medium
 * @return Free and total space in long[]
 */
@Deprecated
private static long[] getStorageDetailsForPath(File path) {
    if (path == null) return new long[]{};
    final int KB = 1024;
    final int MB = KB * 1024;
    long free;
    long total;
    long blockSize;
    try {
        StatFs stats = new StatFs(path.getAbsolutePath());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            free = stats.getAvailableBytes() / MB;
            total = stats.getTotalBytes() / MB;
            return new long[]{free, total};
        } else {
            blockSize = (long) stats.getBlockSize();
            free = ((long) stats.getAvailableBlocks() * blockSize) / MB;
            total = ((long) stats.getBlockCount() * blockSize) / MB;
            if (free < 0 || total < 0) return new long[]{};
            return new long[]{free, total};
        }
    } catch (Exception e) {
        return new long[]{};
    }
}
 
Example #3
Source File: RxFileTool.java    From RxTools-master with Apache License 2.0 6 votes vote down vote up
/**
 * 获取磁盘可用空间.
 */
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static long getSDCardAvailaleSize() {
    File path = getRootPath();
    StatFs stat = new StatFs(path.getPath());
    long blockSize, availableBlocks;
    if (Build.VERSION.SDK_INT >= 18) {
        blockSize = stat.getBlockSizeLong();
        availableBlocks = stat.getAvailableBlocksLong();
    } else {
        blockSize = stat.getBlockSize();
        availableBlocks = stat.getAvailableBlocks();
    }
    return availableBlocks * blockSize;
}
 
Example #4
Source File: DiskStat.java    From FileManager with Apache License 2.0 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 #5
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 #6
Source File: CropImage.java    From Pi-Locker with GNU General Public License v2.0 6 votes vote down vote up
public static int calculatePicturesRemaining(Activity activity) {

        try {
            /*if (!ImageManager.hasStorage()) {
                return NO_STORAGE_ERROR;
            } else {*/
        	String storageDirectory = "";
        	String state = Environment.getExternalStorageState();
        	if (Environment.MEDIA_MOUNTED.equals(state)) {
        		storageDirectory = Environment.getExternalStorageDirectory().toString();
        	}
        	else {
        		storageDirectory = activity.getFilesDir().toString();
        	}
            StatFs stat = new StatFs(storageDirectory);
            float remaining = ((float) stat.getAvailableBlocks()
                    * (float) stat.getBlockSize()) / 400000F;
            return (int) remaining;
            //}
        } catch (Exception ex) {
            // if we can't stat the filesystem then we don't know how many
            // pictures are remaining.  it might be zero but just leave it
            // blank since we really don't know.
            return CANNOT_STAT_ERROR;
        }
    }
 
Example #7
Source File: CrashlyticsReportDataCapture.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
private CrashlyticsReport.Session.Device populateSessionDeviceData() {
  final StatFs statFs = new StatFs(Environment.getDataDirectory().getPath());
  final int arch = getDeviceArchitecture();
  final int availableProcessors = Runtime.getRuntime().availableProcessors();
  final long totalRam = CommonUtils.getTotalRamInBytes();
  final long diskSpace = (long) statFs.getBlockCount() * (long) statFs.getBlockSize();
  final boolean isEmulator = CommonUtils.isEmulator(context);
  final int state = CommonUtils.getDeviceState(context);
  final String manufacturer = Build.MANUFACTURER;
  final String modelClass = Build.PRODUCT;

  return CrashlyticsReport.Session.Device.builder()
      .setArch(arch)
      .setModel(Build.MODEL)
      .setCores(availableProcessors)
      .setRam(totalRam)
      .setDiskSpace(diskSpace)
      .setSimulator(isEmulator)
      .setState(state)
      .setManufacturer(manufacturer)
      .setModelClass(modelClass)
      .build();
}
 
Example #8
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 #9
Source File: IOUtils.java    From DoraemonKit with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #10
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 #11
Source File: FileUtils.java    From android-tv-launcher with MIT License 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 #12
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 #13
Source File: FileSizeUtil.java    From styT with Apache License 2.0 5 votes vote down vote up
/**
 * 获取SDCARD剩余存储空间
 *
 * @return
 */
public static long getAvailableExternalMemorySize() {
    if (externalMemoryAvailable()) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return availableBlocks * blockSize;
    } else {
        return ERROR;
    }
}
 
Example #14
Source File: SimpleActivity.java    From stynico with MIT License 5 votes vote down vote up
/**
    * 获得机身可用内存
    *
    * @return
    */
   private String getRomAvailableSize()
{
       File path = Environment.getDataDirectory();
       StatFs stat = new StatFs(path.getPath());
       long blockSize = stat.getBlockSize();
       long availableBlocks = stat.getAvailableBlocks();
       return Formatter.formatFileSize(SimpleActivity.this, blockSize * availableBlocks);
   }
 
Example #15
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 #16
Source File: ProxyUtils.java    From MediaPlayerProxy with Apache License 2.0 5 votes vote down vote up
/**
 * 获取外部存储器可用的空间
 * 
 * @return
 */
static protected long getAvailaleSize(String dir) {
	StatFs stat = new StatFs(dir);// path.getPath());
	long totalBlocks = stat.getBlockCount();// 获取block数量
	long blockSize = stat.getBlockSize();
	long availableBlocks = stat.getAvailableBlocks();
	return availableBlocks * blockSize; // 获取可用大小
}
 
Example #17
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 busySpaceGibi(boolean external)
{
    StatFs statFs = getStats(external);
    long total = (statFs.getBlockCountLong() * statFs.getBlockSizeLong());
    long free  = (statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong());
    long used = (total - free) / GIBIBYTE;
    Log.d(TAG, "used disk space: " + String.valueOf(used/ GIBIBYTE));
    return (int) used;
}
 
Example #18
Source File: AutoErrorReporter.java    From AutoCrashReporter with Apache License 2.0 5 votes vote down vote up
private long getAvailableInternalMemorySize() {
	File path = Environment.getDataDirectory();
	StatFs stat = new StatFs(path.getPath());
	long blockSize = stat.getBlockSize();
	long availableBlocks = stat.getAvailableBlocks();
	return (availableBlocks * blockSize)/(1024*1024);
}
 
Example #19
Source File: SdCardUtils.java    From ViewDebugHelper with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 获得机身内存总大小rom
 * 
 * @return
 */
public static long getRomTotalSize() {
	File path = Environment.getDataDirectory();
	StatFs stat = new StatFs(path.getPath());
	long blockSize = stat.getBlockSize();
	long totalBlocks = stat.getBlockCount();
	return blockSize * totalBlocks;
}
 
Example #20
Source File: UpdateDownloader.java    From update with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example #21
Source File: CommonUtil.java    From HeroVideo-master with Apache License 2.0 5 votes vote down vote up
/**
 * 获取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;
}
 
Example #22
Source File: SDCardUtil.java    From RxAndroidBootstrap with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the available space in megabytes.
 * @return
 */
public static int getAvailableSpaceInMegaBytes() {
    int availableSpace = 0;

    try {
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
        availableSpace = stat.getAvailableBlocks() * stat.getBlockSize() / 1048576;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return availableSpace;
}
 
Example #23
Source File: SDCardUtils.java    From ClockView with Apache License 2.0 5 votes vote down vote up
/**
 * 获取指定路径所在空间的剩余可用容量字节数,单位byte
 * 
 * @param filePath
 * @return 容量字节 SDCard可用空间,内部存储可用空间
 */
public static long getFreeBytes(String filePath)
{
	// 如果是sd卡的下的路径,则获取sd卡可用容量
	if (filePath.startsWith(getSDCardPath()))
	{
		filePath = getSDCardPath();
	} else
	{// 如果是内部存储的路径,则获取内存存储的可用容量
		filePath = Environment.getDataDirectory().getAbsolutePath();
	}
	StatFs stat = new StatFs(filePath);
	long availableBlocks = (long) stat.getAvailableBlocks() - 4;
	return stat.getBlockSize() * availableBlocks;
}
 
Example #24
Source File: StoreUtils.java    From letv with Apache License 2.0 5 votes vote down vote up
public static long getSdcardAvailableSpace(Context context) {
    Exception e;
    if (!isSdcardAvailable()) {
        return -1;
    }
    String sdcardPath = LetvServiceConfiguration.getDownload_path(context);
    File dir = new File(sdcardPath);
    if (!dir.exists()) {
        dir.mkdirs();
    }
    long block = 0;
    long size = 0;
    try {
        StatFs statFs = new StatFs(sdcardPath);
        StatFs statFs2;
        try {
            block = (long) statFs.getAvailableBlocks();
            size = (long) statFs.getBlockSize();
            statFs2 = statFs;
        } catch (Exception e2) {
            e = e2;
            statFs2 = statFs;
            e.printStackTrace();
            return size * block;
        }
    } catch (Exception e3) {
        e = e3;
        e.printStackTrace();
        return size * block;
    }
    return size * block;
}
 
Example #25
Source File: DirectoryManager.java    From phonegap-plugin-loading-spinner with Apache License 2.0 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 #26
Source File: SystemUtils.java    From AppSmartUpdate with Apache License 2.0 5 votes vote down vote up
/**
 * Check how much usable space is available at a given path.
 * 获取指定目录仍可用的空间大小
 *
 * @param path The path to check
 * @return The space available in bytes
 */
@SuppressLint("NewApi")
public static long getAvailableSpace(File path) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        return path.getUsableSpace();
    }
    final StatFs stats = new StatFs(path.getPath());
    return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();
}
 
Example #27
Source File: StatFsHelperTest.java    From fresco with MIT License 5 votes vote down vote up
@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());
}
 
Example #28
Source File: d.java    From letv with Apache License 2.0 5 votes vote down vote up
public static d b(File file) {
    d dVar = new d();
    dVar.a(file);
    StatFs statFs = new StatFs(file.getAbsolutePath());
    long blockSize = (long) statFs.getBlockSize();
    long availableBlocks = (long) statFs.getAvailableBlocks();
    dVar.a(((long) statFs.getBlockCount()) * blockSize);
    dVar.b(blockSize * availableBlocks);
    return dVar;
}
 
Example #29
Source File: MemoryInfo.java    From MobileInfo with Apache License 2.0 5 votes vote down vote up
private static String getSdcardSizeTotal(Context context) {

        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockCount = stat.getBlockCount();
        long blockSize = stat.getBlockSize();
        return Formatter.formatFileSize(context, blockCount * blockSize);
    }
 
Example #30
Source File: RNDeviceModule.java    From react-native-device-info with MIT License 5 votes vote down vote up
@ReactMethod(isBlockingSynchronousMethod = true)
public double getTotalDiskCapacitySync() {
  try {
    StatFs root = new StatFs(Environment.getRootDirectory().getAbsolutePath());
    return BigInteger.valueOf(root.getBlockCount()).multiply(BigInteger.valueOf(root.getBlockSize())).doubleValue();
  } catch (Exception e) {
    return -1;
  }
}