类android.os.StatFs源码实例Demo

下面列出了怎么用android.os.StatFs的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: easydeviceinfo   文件: EasyMemoryMod.java
/**
 * 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;
  }
}
 
源代码2 项目: android-tv-launcher   文件: FileUtils.java
/**
 * 计算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);
}
 
源代码3 项目: Pi-Locker   文件: CropImage.java
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;
        }
    }
 
源代码4 项目: RxTools-master   文件: RxFileTool.java
/**
 * 获取磁盘可用空间.
 */
@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;
}
 
源代码5 项目: mobile-manager-tool   文件: StorageUtil.java
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;
    }
}
 
源代码6 项目: DoraemonKit   文件: IOUtils.java
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;
}
 
源代码7 项目: android-storage   文件: Storage.java
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();
}
 
源代码8 项目: XKnife-Android   文件: SDCardUtils.java
/**
 * 获取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();
}
 
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();
}
 
源代码10 项目: FileManager   文件: DiskStat.java
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();
        }
    }
}
 
源代码11 项目: batteryhub   文件: Storage.java
/**
 * 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[]{};
    }
}
 
源代码12 项目: aedict   文件: DownloaderService.java
/**
 * 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;
}
 
源代码13 项目: Android-Audio-Recorder   文件: Storage.java
public long getFree(File f) {
    while (!f.exists())
        f = f.getParentFile();

    StatFs fsi = new StatFs(f.getPath());
    if (Build.VERSION.SDK_INT < 18)
        return fsi.getBlockSize() * fsi.getAvailableBlocks();
    else
        return fsi.getBlockSizeLong() * fsi.getAvailableBlocksLong();
}
 
源代码14 项目: medialibrary   文件: GalleryUtils.java
public static boolean hasSpaceForSize(long size) {
    String state = Environment.getExternalStorageState();
    if (!Environment.MEDIA_MOUNTED.equals(state)) {
        return false;
    }

    String path = Environment.getExternalStorageDirectory().getPath();
    try {
        StatFs stat = new StatFs(path);
        return stat.getAvailableBlocks() * (long) stat.getBlockSize() > size;
    } catch (Exception e) {
        Log.i(TAG, "Fail to access external storage", e);
    }
    return false;
}
 
@SuppressWarnings("ObsoleteSdkInt")
private long getAvailableBlocksLong(final @NotNull StatFs stat) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    return stat.getAvailableBlocksLong();
  }
  return getAvailableBlocksDep(stat);
}
 
源代码16 项目: FanXin-based-HuanXin   文件: StatFsHelper.java
/**
 * 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
 */
public long getAvailableStorageSpace(StorageType storageType) {
  ensureInitialized();

  maybeUpdateStats();

  StatFs statFS = storageType == StorageType.INTERNAL ? mInternalStatFs : mExternalStatFs;
  if (statFS != null) {
    long blockSize = statFS.getBlockSize();
    long availableBlocks = statFS.getAvailableBlocks();
    return blockSize * availableBlocks;
  }
  return 0;
}
 
/**
 * Get the total amount of external storage, in bytes, or null if no external storage is mounted.
 *
 * @return the total amount of external storage, in bytes, or null if no external storage is
 *     mounted
 */
private @Nullable Long getTotalExternalStorage(final @NotNull StatFs stat) {
  try {
    long blockSize = getBlockSizeLong(stat);
    long totalBlocks = getBlockCountLong(stat);
    return totalBlocks * blockSize;
  } catch (Exception e) {
    logger.log(SentryLevel.ERROR, "Error getting total external storage amount.", e);
    return null;
  }
}
 
源代码18 项目: SimplifyReader   文件: SDCardManager.java
@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) {

	}
}
 
源代码19 项目: LiTr   文件: DiskUtil.java
/**
 * This method returns the available disk space in data directory, measured in bytes,
 * for the application.
 *
 * @return free disk space in bytes, or FREE_DISK_SPACE_CHECK_FAILED if cannot be determined.
 */
@SuppressWarnings("IllegalCatch")
public long getAvailableDiskSpaceInDataDirectory() {
    try {
        StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsolutePath());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            return statFs.getAvailableBytes();
        } else {
            return (long) statFs.getAvailableBlocks() * statFs.getBlockSize();
        }
    } catch (Exception e) {
        Log.e(TAG, "Could not get Available Disk Space");
        return FREE_DISK_SPACE_CHECK_FAILED;
    }
}
 
源代码20 项目: Aria   文件: FileUtil.java
/**
 * sdcard 可用大小
 *
 * @param sdcardPath sdcard 根路径
 * @return 单位为:byte
 */
private static long getAvailableExternalMemorySize(String sdcardPath) {
  StatFs stat = new StatFs(sdcardPath);
  long blockSize = stat.getBlockSize();
  long availableBlocks = stat.getAvailableBlocks();
  return availableBlocks * blockSize;
}
 
源代码21 项目: zidoorecorder   文件: FileOperate.java
public static long getTotalSize(String path) {
	try {
		StatFs statfs = new StatFs(path);
		long totalBlocks = statfs.getBlockCount();
		long blockSize = statfs.getBlockSize();
		long totalsize = blockSize * totalBlocks;
		return totalsize;
	} catch (Exception e) {
		// TODO: handle exception
	}
	return 0;
}
 
源代码22 项目: android-open-project-demo   文件: OtherUtils.java
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;
    }

}
 
源代码23 项目: fdroidclient   文件: Utils.java
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();
    }
}
 
源代码24 项目: letv   文件: StoreUtils.java
public static long getTotalSpaceByPath(String path) {
    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 statFs = new StatFs(path);
        StatFs statFs2;
        try {
            block = (long) statFs.getBlockCount();
            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;
}
 
源代码25 项目: Kernel-Tuner   文件: Utility.java
public static long getAvailableSpaceInBytesOnInternalStorage()
{
    long availableSpace;
    StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
    availableSpace = (long) stat.getAvailableBlocks()
            * (long) stat.getBlockSize();

    return availableSpace;
}
 
源代码26 项目: QiQuYing   文件: ToolsUtils.java
/**
 * 查看SD卡总容量
 * @return
 */
public long getSDAllSize(){
      //取得SD卡文件路径
      File path = Environment.getExternalStorageDirectory(); 
      StatFs sf = new StatFs(path.getPath()); 
      //获取单个数据块的大小(Byte)
      long blockSize = sf.getBlockSize(); 
      //获取所有数据块数
      long allBlocks = sf.getBlockCount();
      //返回SD卡大小
      return (allBlocks * blockSize)/1024/1024; //单位MB
    }
 
源代码27 项目: DoraemonKit   文件: Utils.java
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);
}
 
源代码28 项目: AndroidNetwork   文件: BaseUtils.java
/**
 * 获取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;
}
 
源代码29 项目: FileManager   文件: DiskStat.java
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();
    }
}
 
源代码30 项目: cathode   文件: ImageModule.java
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);
}
 
 类所在包
 同包方法