Auxiliary classes related to SD card:
1, judge whether SDcard is available;
2, obtain the SD card path;
3. Obtain the remaining capacity of SD card, unit byte;
4. Gets the remaining usable capacity bytes in the space of the specified path, in units of byte;
5, obtain the system storage path;
1 ImportJava.io.File;2 Importandroid.os.Environment;3 ImportAndroid.os.StatFs;4 5 //auxiliary classes related to SD card6 Public classsdcardutils7 {8 Privatesdcardutils ()9 {Ten /*cannot be instantiated*/ One Throw NewUnsupportedoperationexception ("cannot be instantiated"); A } - - /** the * Determine if SDcard is available - * - * @return - */ + Public Static Booleanissdcardenable () - { + returnenvironment.getexternalstoragestate (). Equals ( A environment.media_mounted); at - } - - /** - * Get SD card path - * in * @return - */ to Public StaticString Getsdcardpath () + { - returnenvironment.getexternalstoragedirectory (). GetAbsolutePath () the+File.separator; * } $ Panax Notoginseng /** - * Get the remaining capacity of SD card, unit byte the * + * @return A */ the Public Static Longgetsdcardallsize () + { - if(Issdcardenable ()) $ { $StatFs stat =NewStatFs (Getsdcardpath ()); - //get the number of free data blocks - LongAvailableblocks = (Long) Stat.getavailableblocks ()-4; the //gets the size of a single data Block (byte) - LongFreeblocks =stat.getavailableblocks ();Wuyi returnFreeblocks *availableblocks; the } - return0; Wu } - About /** $ * Gets the remaining available capacity bytes in the space of the specified path, in units of byte - * - * @paramFilePath - * @returncapacity bytes SDcard free space, internal storage free space A */ + Public Static Longgetfreebytes (String filePath) the { - //if it is the path below the SD card, get the available capacity of the SD card $ if(Filepath.startswith (Getsdcardpath ())) the { theFilePath =Getsdcardpath (); the}Else the{//gets the available capacity of the memory store if it is an internal stored path -FilePath =environment.getdatadirectory (). GetAbsolutePath (); in } theStatFs stat =NewStatFs (filePath); the LongAvailableblocks = (Long) Stat.getavailableblocks ()-4; About returnStat.getblocksize () *availableblocks; the } the the /** + * Get System storage Path - * the * @returnBayi */ the Public StaticString Getrootdirectorypath () the { - returnenvironment.getrootdirectory (). GetAbsolutePath (); - } the the}
Android development tool Class 08_sdcardutils