The method of judging the state of SD card by Android programming _android

Source: Internet
Author: User

This paper illustrates the method of the diagnosis of SD card state by Android programming. Share to everyone for your reference, specific as follows:

First we need to increase the SD card access in Androidmanifest.xml:

<!--Mount Rights in SDcard-->
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/ >
<!--Write data permissions to SDcard-->
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_ STORAGE "/>

Then we write a generic class to hold the SD card associated with the operation and status check:

Import Java.io.File; 
Import android.os.Environment; 
Import Android.os.StatFs; 
  public class Sdtool {private static sdtool tool = NULL; 
  Check that the SD card is mounted public Boolean sd_exist = false;  
        Private Sdtool () {} public static Sdtool instance () {if (tool = = null) {synchronized (Sdtool.class) { 
          if (tool = = null) {tool = new sdtool (); Tool. 
        Sd_exist = environment.media_mounted. Equals (Environment.getexternalstoragestate ()); 
  }} return tool; 
    Public long getsdfreesize () {//Get SD card file path filename = environment.getexternalstoragedirectory (); 
    Statfs SF = new Statfs (Path.getpath ()); 
    Long blockSize = Sf.getblocksize (); 
    The number of free blocks of data long freeblocks = Sf.getavailableblocks (); Returns the SD card idle size return freeblocks * blockSize; Unit byte is 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.getblocksize (); 
    Gets all data blocks number long allblocks = Sf.getblockcount (); Returns the SD card size return allblocks * blockSize; 

 Unit byte}}

One field is to judge the existence of sd_exist,true representations.

In addition 2 methods one is to obtain the SD card remaining capacity (Byte), the other obtains the total volume. Later on the SD card operation methods can be written in this area. Easy to manage.

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" and "Android Control usage summary."

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.