Guest User

Untitled

a guest
Jun 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. /**
  2. * 计算SD卡可用空间
  3. *
  4. * @return 单位KB
  5. */
  6. public static long getAvailableSize() {
  7.  
  8. String path = Environment.getExternalStorageDirectory().getPath();// 获取SD卡得根路径
  9. StatFs stat = new StatFs(path);
  10. long availableBlocks = stat.getAvailableBlocks();// 获取空闲BLOCK数量
  11. long blockSize = stat.getBlockSize();// 获取BLOCK的大小
  12. long availableSize = availableBlocks * blockSize;
  13. // availableSize/1024 单位KB
  14. // availableSize/1024 /1024 单位MB
  15. return availableSize / 1024;
  16. }
  17.  
  18. /**
  19. * 计算SD总空间大小
  20. *
  21. * @return 单位KB
  22. */
  23. public static long getAllSize() {
  24.  
  25. String path = Environment.getExternalStorageDirectory().getPath();
  26. StatFs stat = new StatFs(path);
  27. long blockSize = stat.getBlockSize();
  28. long availableBlocks = stat.getBlockCount();
  29. return availableBlocks * blockSize / 1024;
  30. }
  31.  
  32.  
  33.  
  34. /** 取SD卡路径 **/
  35. public static String getSDPath() {
  36. File sdDir = null;
  37. boolean sdCardExist = Environment.getExternalStorageState().equals(
  38. android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
  39. if (sdCardExist) {
  40. sdDir = Environment.getExternalStorageDirectory(); //获取根目录
  41. }
  42. if (sdDir != null) {
  43. return sdDir.toString();
  44. } else {
  45. return "";
  46. }
  47. }
Add Comment
Please, Sign In to add comment