路径错误无效:获取外部存储器大小

时间:2014-05-07 11:04:36

标签: android

当我调用它来获取外部存储器详细信息时,我收到如下错误。

05-07 16:55:07.710: E/AndroidRuntime(22624): FATAL EXCEPTION: main java.lang.IllegalArgumentException: Invalid path: /storage/emulated/0
05-07 16:55:07.710: E/AndroidRuntime(22624):    at android.os.StatFs.doStat(StatFs.java:46)
05-07 16:55:07.710: E/AndroidRuntime(22624):    at android.os.StatFs.<init>(StatFs.java:39)

在将我的机器人更新到SAMSUNG Galaxy S3 4.3之前,这是有效的。我遵循了这个post。这是我的代码:

// getting available memory
public static String getAvailableExternalMemorySize() {
    if (externalMemoryAvailableBool()) {
        // File path =
        // Environment.getExternalStorageDirectory().getAbsolutePath();
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
                .getAbsolutePath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return formatSize(availableBlocks * blockSize);

    } else {
        return ERROR;
    }

}

// getting total memory
public static String getTotalExternalMemorySize() {

    if (externalMemoryAvailableBool()) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return formatSize(totalBlocks * blockSize);
    } else {
        return ERROR;
    }
}

我希望获得&amp;外部存储卡的总内存。

2 个答案:

答案 0 :(得分:1)

这是为了帮助那些寻找相同崩溃签名的人。谷歌搜索时问题就出现了,我没有看到任何明确的答案。

@sandrstar的建议是正确的。 我的应用遭遇了同样的崩溃,我只是通过添加READ_EXTERNAL_STORAGE权限解决了这个问题。 虽然我的情况似乎只在4.4设备上发生/报告崩溃了。

这里解释了背后的原因: http://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE

答案 1 :(得分:0)

使用此代码片段可获取内部存储和外部SD卡的大小

private Long getDiskSpaceTotalInBytes() {
    StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
    stat.restat(Environment.getDataDirectory().getPath());
    return stat.getBlockSizeLong() * stat.getBlockCountLong();
}

private Long getDiskSpaceAvailableInBytes() {
    StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
    stat.restat(Environment.getDataDirectory().getPath());
    return stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
}

private static boolean hasRealRemovableSdCard(Context context) {
    File[] dirs = context.getExternalFilesDirs("");
    if (dirs.length >= 2 && dirs[1] != null) {
        return Environment.isExternalStorageRemovable(dirs[1]);
    }
    return false;
}

private Long getSdCardSpaceAvailableInBytes(Context mContext) {
    if (!hasRealRemovableSdCard(mContext)) {
        return null;
    }
    File[] dirs = mContext.getExternalFilesDirs("");
    StatFs stat = new StatFs(dirs[1].getPath());
    long blockSize = stat.getBlockSizeLong();
    long totalBlocks = stat.getBlockCountLong();
    return totalBlocks * blockSize;
}

private Long getSdCardSpaceTotalInBytes(Context mContext) {
    if (!hasRealRemovableSdCard(mContext)) {
        return null;
    }
    File[] dirs = mContext.getExternalFilesDirs("");
    StatFs stat = new StatFs(dirs[1].getPath());
    long blockSize = stat.getBlockSizeLong();
    long availableBlocks = stat.getAvailableBlocksLong();
    return availableBlocks * blockSize;
}