在android中读取外部SD卡中的可用空间

时间:2017-01-12 08:33:34

标签: android android-external-storage

我写方法但它只返回内部存储空间。但我希望SD卡内存可用空间大小。

import csv

d_1 = []
with open('1.csv') as f_1:
    for row in csv.reader(f_1):
        d_1.append(row[1:])

with open('2.csv') as f_2, open('out.csv', 'wb') as f_out:
    csv_out = csv.writer(f_out)

    for i, row in enumerate(csv.reader(f_2)):
        row.extend(d_1[i])    
        csv_out.writerow(row)

//此处以MB格式返回

  public static String getAvailableExternalMemorySize() {
            if (externalMemoryAvailable()) {
                File path = Environment.getExternalStorageDirectory();
                StatFs stat = new StatFs(path.getPath());
                long blockSize = stat.getBlockSizeLong();
                long availableBlocks = stat.getAvailableBlocksLong();
                return formatSize(availableBlocks * blockSize);
            } else {
                return "Exception";
            }
        }

//检查外部存储器是否存在。

 public static String formatSize(long size) {
        String suffix = null;

        if (size >= 1024) {
            suffix = "KB";
            size /= 1024;
            if (size >= 1024) {
                suffix = "MB";
                size /= 1024;
            }
        }

        StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

        int commaOffset = resultBuffer.length() - 3;
        while (commaOffset > 0) {
            resultBuffer.insert(commaOffset, ',');
            commaOffset -= 3;
        }

        if (suffix != null) resultBuffer.append(suffix);
        return resultBuffer.toString();
    }

0 个答案:

没有答案