Android detects memory cards and the amount of space left on the phone (based on Environment,statfs and DecimalFormat) _android

Source: Internet
Author: User

This article is an example of how Android detects memory cards and the amount of space left in the phone. Share to everyone for your reference, specific as follows:

Android environment can be used to detect whether a phone has a memory card installed and a file storage path. Statfs can get the size of the memory card and the amount of space remaining. DecimalFormat can be achieved by dividing the number into a certain format.

The specific procedures are as follows:

Import Java.io.File;
Import Java.text.DecimalFormat;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.os.StatFs;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.ProgressBar;
Import Android.widget.TextView; public class A08activity extends activity {Private TextView tv;//used to display memory card private Button b;//trigger detect memory card event private Progr The Essbar pb;//uses ProgressBar to display the status of the memory card/** called when the "activity is".
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    tv= (TextView) Findviewbyid (r.id.tv);
    b= (Button) Findviewbyid (R.id.button);
    pb= (ProgressBar) Findviewbyid (R.ID.PB); B.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated Method St
  UB Showsize ();//To detect storage card storage}}); } protected void Showsize (){//TODO auto-generated Method stub Tv.settext ("");
 Pb.setprogress (0);
  Used to detect if the memory card exists if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//If the memory card exists, get the path to the stored file
  File path=environment.getexternalstoragedirectory (); Statfs sf=new Statfs (Path.getpath ());//Create Statfs Object long blocksize=sf.getblocksize ();//Get blockSize long totalblock=
  Sf.getblockcount ();//Get all block long availableblock=sf.getavailableblocks ();//Get available block//string array to store block information
  String[] Total=filesize (totalblock*blocksize);
  String[] Available=filesize (availableblock*blocksize);
  Displays the size of the available space in ProgressBar int a=integer.parseint (available[0]);
  Pb.setprogress (a);
  String s= "SD card space in total:" +total[0]+total[1]+ "\ n";
  s+= "Remaining space size:" +available[0]+available[1];
 Tv.settext (s);
 else if (environment.getexternalstoragestate (). Equals (environment.media_removed)) {tv.settext ("SD card has been removed");
 }///used to define storage space display format public string[] FileSize (long size) {String s= "";
  if (size>1024) {s= "KB"; size/= 1024;
  if (size>1024) {s= "MB";
  size/=1024;
 } DecimalFormat df=new DecimalFormat ();
 Df.setgroupingsize (3);
 String[] Result=new string[3];
 Result[0]=df.format (size);
 Result[1]=s;
 return result;

 }
}

More interested readers of Android-related content can view this site: "The summary of Android controls usage" and "Android Development introduction and Advanced Course"

I hope this article will help you with the Android program.

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.