gpt4 book ai didi

java - 为什么此代码会错误地报告我的 Android 设备上的可用空间?

转载 作者:太空宇宙 更新时间:2023-11-03 13:27:39 25 4
gpt4 key购买 nike

我的带有 32GB SD 卡的三星 X-Cover2 GT-S7710 在“设置”>“存储”中报告了正确的空间,但使用此代码或 How can I check how much free space an SD card mounted on an Android device has? 时只有 0.6 GB :

sdStorageDirectory = Environment.getExternalStorageDirectory().getPath();
StatFs statSd = new StatFs(sdStorageDirectory);
Log.d(TAG, "gb available " + String.valueOf((double) (statSd.getAvailableBlocks() * statSd.getBlockSize()) / 1073741824));

这是为什么,如何才能正确发现真正的空闲空间?

最佳答案

此行为的原因可能是在您的手机中,SD 卡不被视为主要的外部存储设备。 getExternalStorageDirectory()的说明解释这个:

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

所以您可能得到了其他一些真正有 0.6 GB 大小的系统。

幸运的是,您正在寻找的设备与您正在寻找的设备安装在同一父目录中的可能性很高。所以首先,我会检查这是否是您正在寻找的设备,类似 isExternalStorageRemovable()如果不是这样,请尝试同一父目录中的其他设备:

File sdStorage;
if(sdStorage.isExternalStorageRemovable()) {
sdStorage = Environment.getExternalStorageDirectory();
} else {
List<File> storages = Environment.getExternalStorageDirectory().getParentFile().listFiles();
for(File storage : storages) {
if(! storages.equals(Environment.getExternalStorageDirectory()) {
sdStorage = storage;
break;
}
}
}
if(sdStorage == null) {
// No non-removable storage device was found
}

假设只有两个设备已安装。在极少数情况下,这可能不是真的(例如,因为通过 USB-OTG 安装了另一个存储设备);你必须在这里找到另一个解决方法。一种想法可能是运行 mount 命令,该命令描述为 here通过一个过程并通过这些结果来找到正确的设备。然而,这将变得相当复杂。它还假定所有外部存储设备都安装在同一父目录下(通常是 /mnt/)。如果情况并非如此,mount 命令再次成为您最大的希望。

编辑 1: 可能有些设备在不使用 /mnt 目录的情况下挂载到 /sdcard(尽管在某些情况下可能会这样)是对 /mnt/sdcard 的引用,在这种情况下你会没事的)。因此,在这些情况下,您必须使用 mount 的输出。

编辑 2: 我检查后发现,在我的手机上,/mnt 包含很多目录,而我只想要其中一个。所以我想出了这个解决方案而不是上面的解决方案:

File sdStorage;
if(sdStorage.isExternalStorageRemovable()) {
sdStorage = Environment.getExternalStorageDirectory();
} else {
Process process = null;
Scanner scan = null;
try {
process = new ProcessBuilder().command("ls", "-l", sdStorage.getParent()).redirectErrorStream(true).start();
scan = new Scanner(process.getInputStream());
while(scan.hasNextLine()) {
String line = scan.nextLine();
String[] parts = line.split("\\s");
if(parts.length > 2 && parts[2].equals("sdcard_rw")
&& ! Environment.getExternalStorageDirectory().equals(Environment.getExternalStorageDirectory().getParent() + File.seperator + parts[3]) {
sdStorage = new File(Environment.getExternalStorageDirectory().getParent() + File.seperator + parts[3]);
break;
}
} catch(IOException e) {
// Do something here
} finally {
if(scan != null)
scan.close();
if(process != null)
process.destroy();
}
}
}

这里的背景是,所有外部存储设备似乎都属于用户 sdcard_rw 所以这是我们可以检查的。遗憾的是,在 Android 中检查文件所有权的最短方法似乎是……好吧,不是很直观。但希望这应该独立于各种存储设备的确切安装位置。

关于java - 为什么此代码会错误地报告我的 Android 设备上的可用空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17203615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com