Hi,

   I am wondering how to find out how much space is left on the device  
before I download something big?

   According to the documentation StatFs would have the appropriate  
methods for doing that, but in practice I failed with StatFs on the  
emulator and G1.

   Here is a piece of code using those methods from within an Activity:

        String path = getFilesDir().getAbsolutePath();
        StatFs statFs = new StatFs(path);
        
        Log.d("X", "Free Blocks      (1) =" + statFs.getFreeBlocks());
        Log.d("X", "Available Blocks (1) =" + statFs.getAvailableBlocks());

        byte[] oneMegaByte = new byte[1024 * 1024];
        FileOutputStream fos = openFileOutput("dummy", Context.MODE_PRIVATE);
        for (int i = 0; i < 10; i++)
        fos.write(oneMegaByte);
        fos.close();

        // statFs.restat(path); -> makes no difference
        Log.d("X", "Free Blocks      (2) =" + statFs.getFreeBlocks());
        Log.d("X", "Available Blocks (2) =" + statFs.getAvailableBlocks());


   It uses the above mentioned functionality to print how many blocks  
are free/available on the device, then writes a 10MB file and checks  
the free/available blocks again. Unfortunately without any success:


D/X       (  401): Free Blocks      (1) =9582
D/X       (  401): Available Blocks (1) =9582
D/dalvikvm(  401): GC freed 681 objects / 46160 bytes in 69ms
I/dalvikvm-heap(  401): Grow heap (frag case) to 4.001MB for 1048592- 
byte allocation
D/dalvikvm(  401): GC freed 190 objects / 10096 bytes in 66ms
D/X       (  401): Free Blocks      (2) =9582
D/X       (  401): Available Blocks (2) =9582


   The file has been created though:

# pwd
/data/data/test.listview/files
# ls -l
-rw-rw---- app_20   app_20   10485760 2009-01-14 14:59 dummy
# df
/dev: 47284K total, 0K used, 47284K available (block size 4096)
/sqlite_stmt_journals: 4096K total, 0K used, 4096K available (block  
size 4096)
/system: 65536K total, 43424K used, 22112K available (block size 4096)
/data: 65536K total, 37448K used, 28088K available (block size 4096)
/cache: 65536K total, 1156K used, 64380K available (block size 4096)
/sdcard: 258064K total, 115K used, 257949K available (block size 512)
#


   When using df I can see a difference though:

# df
..
/data: 65536K total, 37448K used, 28088K available (block size 4096)
..
# rm dummy
# df
..
/data: 65536K total, 27208K used, 38328K available (block size 4096)
..

   Any ideas how to do that properly?

Cheers,
Mariano


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to