2012-06-06 53 views
3

得到數據分區的大小,我使用有測試代碼:通過編碼

$ 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) 

看到的數據共有65536k和等等,我的問題是如何通過編碼得到的大小,如果我需要root?對嗎?你能給我一些建議嗎?

回答

4
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(); 

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

您不需要root權限。 您可以通過使用得到它:

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 
+0

我只想要得到的數據大小的尺寸,謝謝 – pengwang

+0

上述方法僅返回數據的大小。上述方法可能是實施的簡單選擇。 – Shrikant

+0

不,我有測試totalMemory()我得到2887664,但DT /數據我得到65536K – pengwang