This article is an example of how Android obtains SD card and cell phone ROM capacity. Share to everyone for your reference, specific as follows:
Here is a simple example to get the capacity of the SD card and the capacity of the cell phone ROM, the code is as follows:
Package com.urovo.sdcardspace;
Import Java.io.File;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.os.StatFs;
Import android.app.Activity;
Import Android.text.format.Formatter;
Import Android.view.Menu;
Import Android.widget.TextView; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {super.
OnCreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
TextView TV = (TextView) Findviewbyid (r.id.tv); File path = Environment.getexternalstoragedirectory ();//Get SD card path statfs stat = new Statfs (Path.getpath ());//Create Statfs Object
, used to get the state of the file system long Blockcount = Stat.getblockcount ();
Long blockSize = Stat.getblocksize ();
Long availableblocks = Stat.getavailableblocks (); String totalsize = Formatter.formatfilesize (Getapplicationcontext (), blockcount*blocksize);//Format get SD card Total capacity String availablesize = Formatter.formatfilesize (Getapplicationcontext (), blockcount*availableblocks)//obtain SD card usable capacity Tv.settext ("SD card Total Capacity:" +totalsize+ "\NSD card usable Capacity:" +availablesize+ "\ n" +getromspace ());
Private String Getromspace () {File path = Environment.getdatadirectory ();
Statfs stat = new Statfs (Path.getpath ());
Long Blockcount = Stat.getblockcount ();
Long blockSize = Stat.getblocksize ();
Long availableblocks = Stat.getavailableblocks ();
String totalsize = formatter.formatfilesize (Getapplicationcontext (), blockcount*blocksize);
String availablesize = formatter.formatfilesize (Getapplicationcontext (), blockcount*availableblocks);
Return "Mobile ROM Total capacity:" +totalsize+ "\ Phone rom usable capacity:" +availablesize;
}
}
Specific content, you can analyze the Android settings part of the source code, to deepen understanding.
For more information on Android-related content readers can view the site: "Android programming development of the SD card operation method Summary", "Android Development introduction and Advanced Course", "Android Resource Operation skills Summary", "Android View tips Summary" and "The Android Control usage Summary"
I hope this article will help you with the Android program.