Android development tool Class 08_sdcardutils

Source: Internet
Author: User

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

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.