Tools related to SD card operation
Package Com.flyou.utils;import java.io.file;import android.os.environment;import android.os.statfs;/** * SD Card Related AUXILIARY class * * * */public class Sdcardutils{private Sdcardutils () {/* cannot be instantiated */throw new Unsupportedoperationexception (" Cannot be instantiated ");} /** * Determine if SDcard is available * * @return */public static Boolean issdcardenable () {return environment.getexternalstoragestate (). equ ALS (environment.media_mounted);} /** * Get SD card path * * @return */public static String Getsdcardpath () {return environment.getexternalstoragedirectory (). Getabs Olutepath () + file.separator;} /** * Get the remaining capacity of the SD card unit byte * * @return */public static long Getsdcardallsize () {if (issdcardenable ()) {StatFs stat = new STATF S (Getsdcardpath ());//Gets the number of idle data blocks long availableblocks = (long) stat.getavailableblocks ()-4;//Gets the size of a single data block (byte) long Freeblocks = Stat.getavailableblocks (); return freeblocks * availableblocks;} return 0;} /** * Gets the remaining available capacity bytes in the space of the specified path, in byte * * @param filePath * @return capacity byte sdcard free space, internal storage free space */public StatiC Long Getfreebytes (String filePath) {//If it is the path under the SD card, get the SD card usable capacity if (Filepath.startswith (Getsdcardpath ())) {FilePath = Getsdcardpath ();} else{//if it is an internal stored path, obtain the available capacity of the memory store filepath = Environment.getdatadirectory (). GetAbsolutePath (); StatFs stat = new StatFs (filePath); Long availableblocks = (long) stat.getavailableblocks ()-4;return stat.getblocksize () * Availableblocks;} /** * Gets the system storage path * * @return */public static String Getrootdirectorypath () {return environment.getrootdirectory (). Getabsolu Tepath ();}}
SD card operation related tools Sdcardutils