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.