Android Get storage space

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.