Package com.example.getMem;
Import Java.io.File;
Import Android.os.Build;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.os.StatFs;
Import Android.text.format.Formatter;
Import Android.widget.TextView;
Import android.app.Activity;
Import Android.app.AlertDialog.Builder;
public class Mainactivity extends Activity {
Private TextView tv_totalsize;
Private TextView tv_availabelsize;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Tv_totalsize= (TextView) Findviewbyid (r.id.totalsize);
Tv_availabelsize= (TextView) Findviewbyid (R.id.availabel);
Get storage space
File datafiledir=environment.getdatadirectory ();
String data =getmemoryinfo (DATAFILEDIR);
String []d= Data.split ("# #");
Tv_totalsize.settext ("Total internal storage size:" + d[0]);
Tv_availabelsize.settext ("Internal storage available space is:" + d[1]);
Get SD card Space
File sdfiledir=environment.getexternalstoragedirectory ();
Determine if the SD card is available
if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {
String sdmemory =getmemoryinfo (Sdfiledir);
String []sd=sdmemory.split ("# #");
Tv_totalsize.settext ("Total internal storage size:" + sd[0]);
Tv_availabelsize.settext ("Internal storage available space is:" + sd[1]);
//
// }
}
Private String Getmemoryinfo (File path) {
Long blockSize;
Long Totalblockcount;
Long Availabelcount;
TODO auto-generated Method Stub
StatFs stat = new StatFs (Path.getpath ());
Detection System version
if (Build.VERSION.SDK_INT >=build.version_codes. JELLY_BEAN_MR2) {
Get the size of each sector
BlockSize = Stat.getblocksizelong ();
Get how many sectors are in total
Totalblockcount=stat.getblockcountlong ();
Get the number of available sectors
Availabelcount=stat.getavailableblockslong ();
}else{
Blocksize=stat.getblocksize ();
Totalblockcount=stat.getblockcount ();
Availabelcount=stat.getavailableblocks ();
}
Total disk size
String totalmemory =formatter.formatfilesize (this, blocksize*totalblockcount);
Available Sizes
String Availabelmemory=formatter.formatfilesize (this, blocksize*availabelcount);
Return totalmemory+ "# #" +availabelmemory;
}
}
http://pan.baidu.com/s/1bp0V87d
Android Get storage space