Method of obtaining path and residual capacity of SD card by Android programming

  • 2021-07-01 08:12:27
  • OfStack

This paper describes the method of obtaining SD card path and remaining capacity by Android programming. Share it for your reference, as follows:


public static String getExternalStoragePath() {
 //  Get SdCard Status 
 String state = android.os.Environment.getExternalStorageState();
 //  Judge SdCard Does it exist and is available 
 if (android.os.Environment.MEDIA_MOUNTED.equals(state)) {
 if (android.os.Environment.getExternalStorageDirectory().canWrite()) {
   return android.os.Environment.getExternalStorageDirectory().getPath();
 }
 }
 return null;
}
public static long getAvailableStore(String filePath) {
 //  Acquire sdcard File path 
 StatFs statFs = new StatFs(filePath);
 //  Get block Adj. SIZE
 long blocSize = statFs.getBlockSize();
 //  Get BLOCK Quantity 
 // long totalBlocks = statFs.getBlockCount();
 //  Available Block Quantity of 
 long availaBlock = statFs.getAvailableBlocks();
 // long total = totalBlocks * blocSize;
 long availableSpare = availaBlock * blocSize;
 return availableSpare;
}

For more readers interested in Android related content, please check the topics on this site: "Summary of SD Card Operation Methods for Android Programming Development", "Introduction and Advanced Tutorial for Android Development", "Summary of Android Resource Operation Skills", "Summary of View Skills for Android View" and "Summary of Android Control Usage"

I hope this article is helpful to everyone's Android programming.


Related articles: