Android programming to determine the existence of SD card and the use of capacity query implementation method _android

Source: Internet
Author: User

This paper illustrates the existence of the SD card and the implementation of capacity query using the Android programming method. Share to everyone for your reference, specific as follows:

1. To determine whether the existence of SD card returns TRUE indicates existence

/* To determine if SD card exists returns true to exist
/public boolean Avaiablemedia () {
  String status = Environment.getexternalstoragestate ();
  if (Status.equals (environment.media_mounted)) {return
   true;
  } else {return
   false;
}}

2. Get SD card free space

/* Get SD card free space *
/public long getsdfreesize () {
  //Get SD card file path filename
  Environment.getexternalstoragedirectory ();
  Statfs SF = new Statfs (Path.getpath ());
  Gets the size of a single block of data (Byte)
  Long blockSize = Sf.getblocksizelong ();
  The number of free blocks of data
  long freeblocks = Sf.getavailableblockslong ();
  Back to SD card idle size
  //return freeblocks * blockSize;//unit byte
  //Return (Freeblocks * blockSize)/1024;//unit KB
  RE Turn (Freeblocks * blockSize)/1024/1024; per MB
}

3. Get SD card all the space

/* Get SD Card all space/public
long Getsdallsize () {
  //Get SD card file path filename
  Environment.getexternalstoragedirectory ();
  Statfs SF = new Statfs (Path.getpath ());
  Gets the size of a single block of data (Byte)
  Long blockSize = Sf.getblocksizelong ();
  Gets all data blocks number
  long allblocks = Sf.getblockcountlong ();
  Back to SD card size
  //return allblocks * blockSize;//unit byte
  //Return (Allblocks * blockSize)/1024;//unit KB
  return (Allblocks * blockSize)/1024/1024; per MB
}

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

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.