获取内部和外部存储信息 - android

时间:2017-02-17 12:34:28

标签: android file-manager

我正在开发一个Android文件管理器应用程序。 所以在主要活动上我想显示所有可用的存储类型,如内部存储和外部SD卡。

所以我使用了这段代码,

public static boolean externalMemoryAvailable() {
    return android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED);
}

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 getTotalInternalMemorySize() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long totalBlocks = stat.getBlockCount();
    return totalBlocks * blockSize;
}

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 0;
    }
}

public static long getTotalExternalMemorySize() {
    if (externalMemoryAvailable()) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return totalBlocks * blockSize;
    } else {
        return 0;
    }
}

但问题是,它为内部和外部存储提供了相同的内存输出。 实际上它给出了内部存储的正确答案。外部SD卡错了。

我认为我错误地获得了外卡的路径。任何帮助? PLZ。

2 个答案:

答案 0 :(得分:1)

是的sd卡位置路径因android的不同品牌而异,无法保证。 我有一个解决方案,但这适用于minSdkVersion 19。

static  File dirs[];
dirs = ContextCompat.getExternalFilesDirs(context, null);
//dirs[0] refers to internal memory and dirs[1] gives you external. Call the following methods to get total and available memory details.


public static String getTotalExternalMemorySize(File dirs[]) {
if (dirs.length > 1) {
        StatFs stat = new StatFs(dirs[1].getPath());
        long blockSize = stat.getBlockSizeLong();
        long totalBlocks = stat.getBlockCountLong();
        return readableFileSize(totalBlocks * blockSize);
    } else {
        return "NA";
}

 public static String getAvailableExternalMemorySize(File[] dirs) {
    if (dirs.length > 1) {
        StatFs stat = new StatFs(dirs[1].getPath());
        long blockSize = stat.getBlockSizeLong();
        long availableBlocks = stat.getAvailableBlocksLong();
        return readableFileSize(availableBlocks * blockSize);
    } else {
        return "NA";
    }
}

 public static String readableFileSize(long size) {
    if(size <= 0) return "0";
    final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" };
    int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
    return new DecimalFormat("#,##0.##").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}

答案 1 :(得分:0)

不要忘记设置外部存储空间 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

另见Accessing getExternalStorageDirectory