简体   繁体   English

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

[英]get internal and external storage information- android

i am working on a android file manager app. 我正在使用android文件管理器应用程序。 so on main activity i want to show the all the available storage types like internal storage and external sd card. 所以在主要活动上,我想显示所有可用的存储类型,例如内部存储和外部SD卡。

so i used this code, 所以我用了这段代码

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

but the problem is, its giving me same memory outputs for both internal and external storage.. actually its giving right answer for internal storage. 但是问题是,它为我提供了相同的内存输出给内部和外部存储。实际上,它为内部存储提供了正确的答案。 but wrong for external sd card. 但外部SD卡错误。

i think i am wrong at getting the path of ext sd card. 我认为我在获取ext SD卡的路径时错了。 any help? 有什么帮助吗? plz.

Yes the sd card location path is different for different makes of android and cannot be guaranteed. 是的,不同品牌的android的SD卡位置路径不同,因此无法保证。 I have a solution but this works with minSdkVersion 19. 我有一个解决方案,但是可以在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];
}

Don't forget to set the permission for external storage <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 不要忘记为外部存储设置权限<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

See also Accessing getExternalStorageDirectory 另请参见访问getExternalStorageDirectory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM