如何获得一个外部存储SD卡的大小(与安装的SD卡)?

链接:我基于这个链接工作

我添加了这一行来查找大小(内部和外部)大小,

return availableExternalMemorySize/(1024*1024); 

我在我的平板电脑上testing。 我得到内部和外部的SD卡大小,

在内部存储器中,1)总内存 – 1007 2)可用内存–683

在外部存储器中,1)总内存 – 1763 2)可用内存 – 1554

但在平板电脑上,我看到设置。一个外部存储大小有8GB。 但是,当我通过编程testing时,它显示了1.7 GB左右。

什么是find外部存储大小的过程?

请有人build议/帮助我?

谢谢 !

Solutions Collecting From Web of "如何获得一个外部存储SD卡的大小(与安装的SD卡)?"

为了让外部SD卡的“空闲”空间显示与“菜单 – >设置 – > SD卡和电话存储号码一致的号码,请使用以下代码:

 StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); double sdAvailSize = (double)stat.getAvailableBlocks() * (double)stat.getBlockSize(); //One binary gigabyte equals 1,073,741,824 bytes. double gigaAvailable = sdAvailSize / 1073741824; 

以下是您如何获得内部存储大小:

  StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath()); long blockSize = statFs.getBlockSize(); long totalSize = statFs.getBlockCount()*blockSize; long availableSize = statFs.getAvailableBlocks()*blockSize; long freeSize = statFs.getFreeBlocks()*blockSize; 

以下是您如何获得外部存储容量(SD卡大小):

  StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); long blockSize = statFs.getBlockSize(); long totalSize = statFs.getBlockCount()*blockSize; long availableSize = statFs.getAvailableBlocks()*blockSize; long freeSize = statFs.getFreeBlocks()*blockSize; 

简短说明

免费街区:

文件系统上空闲的块的总数,包括保留块(普通应用程序不可用)。

可用的块:

文件系统上可用并可用于应用程序的块数。


以下是如何检测SD卡是否安装:

  String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // We can only read the media } else { // No external media } 

相关文档: http : //developer.android.com/reference/android/os/StatFs.html

您可以使用getTotalSpace ()getFreeSpace ()getUsableSpace () https://developer.android.com/reference/java/io/File.html

 import java.io.File; import android.util.Log; .... File f = getMyFile(); Log.d("MyApp", f.getTotalSpace()+""); .... 

为了解释弃用和交叉可用性,我已经制定了这些方法。

 public static long sdCardFree_bytes() { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long free_memory = 0; //return value is in bytes if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) { free_memory = stat.getAvailableBlocksLong() * stat.getBlockSizeLong(); } else { free_memory = stat.getAvailableBlocks() * stat.getBlockSize(); } return free_memory; } public static long sdCardUsed_bytes() { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long free_memory = 0; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) { free_memory = (stat.getBlockCountLong() - stat.getAvailableBlocksLong()) * stat.getBlockSizeLong(); //return value is in bytes } else { free_memory = (stat.getBlockCount() - stat.getAvailableBlocks()) * stat.getBlockSize(); //return value is in bytes } return free_memory; } public static long sdCardTotal_bytes() { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long free_memory = 0; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) { free_memory = stat.getBlockCountLong() * stat.getBlockSizeLong(); //return value is in bytes } else { free_memory = stat.getBlockCount() * stat.getBlockSize(); //return value is in bytes } return free_memory; }