简体   繁体   English

Android:如何获取外部SD卡的存储空间?

[英]Android: How to get storage of external sd-card?

In my device I have 32GB internal memory and 64GB on SD-Card. 在我的设备中,我有32GB的内部存储器和64GB的SD卡。 I like to get the external free size with this method (based on this thread Android get free size of internal/external memory ): 我喜欢使用此方法获取外部可用大小(基于此线程, Android获得内部/外部内存的可用大小 ):

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

When I run this code it only shows me the free storage of internal memory. 当我运行此代码时,它仅显示内部内存的可用存储空间。 Why does it not shows the external SD-Card size? 为什么不显示外部SD卡大小? On Device Storage I have 11GB free and on external SD-Card there are 40GB free. 在设备存储上,我有11GB的可用空间,而在外部SD卡上有40GB的可用空间。

Problem is that there is no api to access external sd cards on android. 问题是没有api可以访问android上的外部SD卡。 On android >= 4.4 external sd cards cannot be written to. 在android> = 4.4上,无法写入外部sd卡。

The only thing you could do (VERY SPECIAL, not recommended for public apps, but doable in a restricted environment!), if you're certain the devices are < android 4.4: Keep a list of paths that you have seen to work so far on many devices and check all of them for existance properly. 您唯一可以做的(非常特殊,不建议在公共应用程序中使用,但可以在受限环境中使用!),如果您确定设备的版本低于android 4.4:请列出迄今为止可以使用的路径列表在许多设备上,并检查所有设备是否正确存在。 Maybe you get lucky. 也许你很幸运。

I found a solution to read the storage of removable sd-card: 我找到了一种读取可移动SD卡存储的解决方案:

This method retourns the free memory size on removable sd-card in byte. 此方法以字节为单位重新计算可移动sd卡上的可用内存大小。

public static long getFreeExternalMemorySize() {
        String secStore = System.getenv("SECONDARY_STORAGE");
        File path = new File(secStore);
        StatFs stat = new StatFs(path.getPath());

        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();

        return availableBlocks * blockSize;
}

You can get an idea after reading the following links. 阅读以下链接后,您可以了解一个想法。

http://developer.android.com/guide/topics/data/data-storage.html#filesExternal http://developer.android.com/guide/topics/data/data-storage.html#files外部

for handling sd card. 用于处理SD卡。 Check whether sd card is mounted before you access. 访问前,请检查是否已安装sd卡。

http://developer.android.com/training/basics/data-storage/files.html#WriteExternalStorage http://developer.android.com/training/basics/data-storage/files.html#WriteExternalStorage

Check sd card is writable and readable. Check SD卡可写且可读。

http://developer.android.com/training/basics/data-storage/files.html#GetFreeSpace http://developer.android.com/training/basics/data-storage/files.html#GetFreeSpace

to get the free space in sdcard or internal storage. 获取sdcard或内部存储中的可用空间。

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

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