pengwang
pengwang

Reputation: 19956

get the size of data partition by coding

i have test code using :

  $ adb -s emulator-5554 shell

   # df
   df
 /dev: 47084K total, 0K used, 47084K available (block size 4096)
/sqlite_stmt_journals: 4096K total, 0K used, 4096K available (block size  4096)
/system: 73600K total, 73600K used, 0K available (block size 4096)
/data: 65536K total, 18464K used, 47072K available (block size 4096)
/cache: 65536K total, 1156K used, 64380K available (block size 4096)

you see the data have total 65536k and so on,my question is how to get the size by coding?if i need root right?can you give me some advice?

Upvotes: 3

Views: 3555

Answers (2)

pengwang
pengwang

Reputation: 19956

try {
 Runtime rt = Runtime.getRuntime();
 Process pcs = rt.exec("df /data");
 BufferedReader br = new BufferedReader(new InputStreamReader(pcs
       .getInputStream()));
 String line = null;
 while ((line = br.readLine()) != null) {
    Log.e("line","line="+line);
 }
 br.close();
 pcs.waitFor();

or

    File path = Environment.getDataDirectory();
    android.os.StatFs stat = new android.os.StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks()*blockSize;

Upvotes: 4

Shrikant Ballal
Shrikant Ballal

Reputation: 7087

You don't need root permission. You can get it by using:

Runtime.getRuntime().freeMemory(); // Returns the amount of free memory resources which are available to the running program.

Runtime.getRuntime().maxMemory(); // Returns the maximum amount of memory that may be used by the virtual machine, or Long.MAX_VALUE if there is no such limit.

Runtime.getRuntime().totalMemory(); // total memory available for your app to run

Upvotes: 0

Related Questions