Android tools ------) SD card-related auxiliary classes

Source: Internet
Author: User

Android tools ------) SD card-related auxiliary classes

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 whether SDCard is available ** @ return */public static boolean isSDCardEnable () {return Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED);}/*** get SD card path ** @ return */public static String getSDCardPath () {return Environment. getExternalStorageDirectory (). getAbsolutePath () + File. separator;}/*** get the remaining capacity unit of the SD Card byte ** @ return */public static long getSDCardAllSize () {if (isSDCardEnable ()) {StatFs stat = new StatFs (getSDCardPath (); // obtain the number of idle data blocks long availableBlocks = (long) stat. getAvailableBlocks ()-4; // obtain the size of a single data block (byte) long freeBlocks = stat. getAvailableBlocks (); return freeBlocks * availableBlocks;} return 0;}/*** get the remaining available capacity bytes of the space in the specified path, unit byte ** @ param filePath * @ return capacity byte SDCard space, internal storage space */public static long getFreeBytes (String filePath) {// if the path is under the SD card, obtain the available capacity of the SD card if (filePath. startsWith (getSDCardPath () {filePath = getSDCardPath ();} else {// if it is an internal storage path, obtain the available memory storage capacity filePath = Environment. getDataDirectory (). getAbsolutePath ();} StatFs stat = new StatFs (filePath); long availableBlocks = (long) stat. getAvailableBlocks ()-4; return stat. getBlockSize () * availableBlocks;}/*** obtain the system storage path ** @ return */public static String getRootDirectoryPath () {return Environment. getRootDirectory (). getAbsolutePath ();}}

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.