Reputation: 6931
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.
Upvotes: 8
Views: 4219
Reputation: 177
Use this snipped to get size of internal storage and external Sdcard
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;
}
Upvotes: 0
Reputation: 171
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
Upvotes: 1