-2

Any idea of how I can read/get the real amount of free space on a sd card? I'm running on Android 4.3. I have tried all possible things to read it correctly, however I'm getting something a bit over twice the real free space.

Now I've tried the classic

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();

using the API 18 getAvailableBlocksLong and other getBlockSizeLong and still the same result.

The only thing that works (but it's not what I need) is the shell command df. I also tried the get usable free space functions with the same result. And this thing doesn't happen on my device only, but on all devices that I try the code.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
user2015552
  • 115
  • 1
  • 2
  • 12
  • Please try using `getAvailableSpaceInMB()` for getting available space in MBs. – Srijith Aug 04 '14 at 09:50
  • Not the formatting is the problem. the problem is that however i try to get the free space, it's wrong. I have 13.31gb free on the sd card, but i get around 24gb. – user2015552 Aug 04 '14 at 10:43

1 Answers1

1

Use this:-

   StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
{ 
@SuppressWarnings("deprecation")
 long sdAvailSize = (long)stat.getAvailableBlocksLong() * (long)stat.getBlockSizeLong(); 
} else
{
 @SuppressWarnings("deprecation") 
double sdAvailSize = (double)stat.getAvailableBlocks() * (double)stat.getBlockSize();
 }

try doing this:-

double gigaAvailable = sdAvailSize / 1073741824;

If this didn't worked then change getAvailableBlocks() to getBlockCount().

you can also try this:-

/**
 * @return Number of gega bytes available on External storage
 */
public static long getAvailableSpaceInGB(){
    final long SIZE_KB = 1024L;
    final long SIZE_GB = SIZE_KB * SIZE_KB * SIZE_KB;
    long availableSpace = -1L;
    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
    availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
    return availableSpace/SIZE_GB;
}

Copied from here :)

Hope it helps.

Community
  • 1
  • 1
Amit Anand
  • 1,225
  • 1
  • 16
  • 40
  • no. still nothing. but i've just noticed the funny thing : i've run "df $EXTERNAL_STORAGE" and it points to the internal storage... and i get the 24gb... that are being faulty displayed as the external ones... i feel like dieing :((( – user2015552 Aug 04 '14 at 11:18
  • man, read my post. this is just what i was saying: the classic stat.get* or stat.get*Long are not working for me. Thanks for helping. – user2015552 Aug 04 '14 at 11:21
  • What does it shows if you check the free space available in internal storage? – Amit Anand Aug 04 '14 at 11:25
  • i get 24gb (in settings->storage...) on the internal; and 13 on the external. df works well without params, but if i use df $EXT... i get shown just the internal storage .../emulated/0 – user2015552 Aug 04 '14 at 11:28
  • have you checked it programmatically? – Amit Anand Aug 04 '14 at 11:30
  • yes. no in'm into su and i'm getting the output of su "df" in my app and also i'm running several free sd card space functions to check this. i've unmounted and mounted back the sdcard and everything is the same. can you send me a test apk that retrieves the sd card free space. maybe i'm having an issue here with something else. – user2015552 Aug 04 '14 at 11:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58607/discussion-between-user2015552-and-help). – user2015552 Aug 04 '14 at 11:40