android 是否可以获得总存储容量?

k97glaaz  于 2023-03-28  发布在  Android
关注(0)|答案(5)|浏览(170)

我知道如何获得内部存储器和外部存储器的可用容量。
但是我想知道最大容量的内部存储器和最大容量的外部存储器,但我找不到任何资料,它在谷歌上
有可能实现吗?
谢谢

sqougxex

sqougxex1#

API 18中引入了一些新方法。这是我用来获取总存储(内部+外部)的代码。
private static final long KILOBYTE = 1024;

StatFs internalStatFs = new StatFs( Environment.getRootDirectory().getAbsolutePath() );
long internalTotal;
long internalFree;

StatFs externalStatFs = new StatFs( Environment.getExternalStorageDirectory().getAbsolutePath() );
long externalTotal;
long externalFree;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    internalTotal = ( internalStatFs.getBlockCountLong() * internalStatFs.getBlockSizeLong() ) / ( KILOBYTE * KILOBYTE );
    internalFree = ( internalStatFs.getAvailableBlocksLong() * internalStatFs.getBlockSizeLong() ) / ( KILOBYTE * KILOBYTE );
    externalTotal = ( externalStatFs.getBlockCountLong() * externalStatFs.getBlockSizeLong() ) / ( KILOBYTE * KILOBYTE );
    externalFree = ( externalStatFs.getAvailableBlocksLong() * externalStatFs.getBlockSizeLong() ) / ( KILOBYTE * KILOBYTE );
}
else {
    internalTotal = ( (long) internalStatFs.getBlockCount() * (long) internalStatFs.getBlockSize() ) / ( KILOBYTE * KILOBYTE );
    internalFree = ( (long) internalStatFs.getAvailableBlocks() * (long) internalStatFs.getBlockSize() ) / ( KILOBYTE * KILOBYTE );
    externalTotal = ( (long) externalStatFs.getBlockCount() * (long) externalStatFs.getBlockSize() ) / ( KILOBYTE * KILOBYTE );
    externalFree = ( (long) externalStatFs.getAvailableBlocks() * (long) externalStatFs.getBlockSize() ) / ( KILOBYTE * KILOBYTE );
}

long total = internalTotal + externalTotal;
long free = internalFree + externalFree;
long used = total - free;
oyjwcjzk

oyjwcjzk2#

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;

来源
此外,使用这个内部大小。

StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
toe95027

toe950273#

这个方法并不适用于所有的手机,大多数手机返回的是用户可用的内存总量,较少的系统内存。你可能会尝试在使用NDK的c++方法中使用'df -h' linux命令,但有些手机只使用系统(su)命令。所以总的来说,你不能。

x7yiwoj4

x7yiwoj44#

StatFs-检索文件系统上空间的总体信息。这是Unix statvfs()的 Package 器。

从API级别18开始,我们使用

long getTotalBytes - The total number of bytes supported by the file system.

对于较旧的API使用

int getBlockCount ()
int getBlockSize ()

StatFs stat = new StatFs(**path**);
long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();

作为路径您使用Environment.getExternalStorageDirectory(),请检查此值。在具有多个共享/外部存储目录的设备中,此目录表示用户将与之交互的主存储。
你需要像这样使用path的字符串值:“/mnt/external_sd/”“/mnt/extSdCard/”
您可以检索所有**/mnt/**设备的列表,并使用其中一个值。

File allMounted = new File("/mnt/");
if(allMounted.isDirectory()){ 
String[] dirs = allMounted.list();...}

或者类似的东西

var myfile=new Java.IO.File("storage/");
var listOfStorages=myfile.ListFiles();

String[] externals = System.getenv("SECONDARY_STORAGE").split(":");
wpx232ag

wpx232ag5#

val stat = StatFs(Environment.getExternalStorageDirectory().path)

    val available = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        stat.blockCountLong * stat.blockSizeLong
    } else {
        stat.blockCount.toLong() * stat.blockSize.toLong()
    }
    val totalInMB = bytesAvailable / (1024*1024)

相关问题