Android每个存储空间有多少可用空间

时间:2015-02-06 10:14:53

标签: android android-sdcard android-memory

enter image description here我想获得设备的(内部和外部)存储空间大小。我在设备上存储了以下内容: 内部存储器, SD卡, 手机存储, enter image description here 我能够获得内部和SD卡存储,但无法获得手机存储空间。请帮忙。

2 个答案:

答案 0 :(得分:1)

请参阅this question,您既可以获得内部存储空间,也可以获得SD卡可用空间。

编辑:来自链接的问题:

public long freeMemory() { 
    StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsolutePath()); 
    long free = (statFs.getAvailableBlocks() * statFs.getBlockSize());
    return free; 
}

转换为人类可读格式(MB,GB等)请参阅喜欢的问题。

通常(无论如何在中国手机上)内部存储分为两部分,一部分仅用于应用和应用数据,另一部分用作SD卡,用于照片,媒体等。有些手机也有SD卡插槽,您也可以访问。

答案 1 :(得分:1)

public static long getAvailableStorage() {
        File path = StorageUtils.getCacheDirectory(ApplicationController.getInstance(), true);
        long blockSize = 0;
        long availableBlocks = 0;
        StatFs stat = new StatFs(path.getPath());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            blockSize = stat.getBlockSizeLong();
            availableBlocks = stat.getAvailableBlocksLong();
        } else {
            blockSize = stat.getBlockSize();
            availableBlocks = stat.getAvailableBlocks();
        }
        return availableBlocks * blockSize;
    }

public static File getCacheDirectory(Context context,
        boolean preferExternal) {
    File appCacheDir = null;
    String externalStorageState;
    try {
        externalStorageState = Environment.getExternalStorageState();
    } catch (NullPointerException e) { // (sh)it happens (Issue #660)
        externalStorageState = "";
    }
    if (preferExternal && MEDIA_MOUNTED.equals(externalStorageState)
        && hasExternalStoragePermission(context)) {
        appCacheDir = getExternalCacheDir(context);
    }
    if (appCacheDir == null) {
        appCacheDir = context.getCacheDir();
    }
    if (appCacheDir == null) {
        String cacheDirPath = "/data/data/" + context.getPackageName()
            + "/"+DOWNLOAD_SUB_DIRECTORY+"/";

        appCacheDir = new File(cacheDirPath);
    }
    return appCacheDir;
    }

private static File getExternalCacheDir(Context context) {
    File dataDir = new File(new File(
        Environment.getExternalStorageDirectory(), "Android"), "data");
    File appCacheDir = new File(
        new File(dataDir, context.getPackageName()), DOWNLOAD_SUB_DIRECTORY);
    if (!appCacheDir.exists()) {
        if (!appCacheDir.mkdirs()) {
        L.w("Unable to create external cache directory");
        return null;
        }
        try {
        new File(appCacheDir, ".nomedia").createNewFile();
        } catch (IOException e) {

        }
    }
    return appCacheDir;
    }