Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid path error: get the external memory size

Tags:

android

I am getting an error like below when I calling this to get External Memory Details.

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)

It was works before i update my android to SAMSUNG Galaxy S3 4.3. I followed this post.This is my Code:

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

I want to get Available & Total Memory of my External Memory Card.

like image 703
Shihab Uddin Avatar asked May 07 '14 11:05

Shihab Uddin


1 Answers

This is to help those who are looking for the same crash signature. Since this question came up on top when googling for it and I didn't see any clear accepted answer.

The suggestion by @sandrstar is correct. My apps experienced the same crash and I've resolved it just by adding READ_EXTERNAL_STORAGE permission. Though it seems crashes for my case only occurred/reported on 4.4 device.

The reason behind is explained here: http://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE

like image 80
oceanuz Avatar answered Nov 17 '22 03:11

oceanuz