SD卡相关→SDCardUtils

   
  import android.annotation.TargetApi;
  import android.os.Build;
  import android.os.Environment;
  import android.os.StatFs;
   
  import java.io.BufferedInputStream;
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.InputStreamReader;
   
  /**
  * <pre>
  * author: Blankj
  * blog : http://blankj.com
  * time : 2016/8/11
  * desc : SD卡相关工具类
  * </pre>
  */
  public final class SDCardUtils {
   
  private SDCardUtils() {
  throw new UnsupportedOperationException("u can't instantiate me...");
  }
   
  /**
  * 判断SD卡是否可用
  *
  * @return true : 可用<br>false : 不可用
  */
  public static boolean isSDCardEnable() {
  return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
  }
   
  /**
  * 获取SD卡路径
  * <p>先用shell,shell失败再普通方法获取,一般是/storage/emulated/0/</p>
  *
  * @return SD卡路径
  */
  public static String getSDCardPath() {
  if (!isSDCardEnable()) return null;
  String cmd = "cat /proc/mounts";
  Runtime run = Runtime.getRuntime();
  BufferedReader bufferedReader = null;
  try {
  Process p = run.exec(cmd);
  bufferedReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getInputStream())));
  String lineStr;
  while ((lineStr = bufferedReader.readLine()) != null) {
  if (lineStr.contains("sdcard") && lineStr.contains(".android_secure")) {
  String[] strArray = lineStr.split(" ");
  if (strArray.length >= 5) {
  return strArray[1].replace("/.android_secure", "") + File.separator;
  }
  }
  if (p.waitFor() != 0 && p.exitValue() == 1) {
  break;
  }
  }
  } catch (Exception e) {
  e.printStackTrace();
  } finally {
  CloseUtils.closeIO(bufferedReader);
  }
  return Environment.getExternalStorageDirectory().getPath() + File.separator;
  }
   
  /**
  * 获取SD卡data路径
  *
  * @return SD卡data路径
  */
  public static String getDataPath() {
  if (!isSDCardEnable()) return null;
  return Environment.getExternalStorageDirectory().getPath() + File.separator + "data" + File.separator;
  }
   
  /**
  * 获取SD卡剩余空间
  *
  * @return SD卡剩余空间
  */
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
  public static String getFreeSpace() {
  if (!isSDCardEnable()) return null;
  StatFs stat = new StatFs(getSDCardPath());
  long blockSize, availableBlocks;
  availableBlocks = stat.getAvailableBlocksLong();
  blockSize = stat.getBlockSizeLong();
  return ConvertUtils.byte2FitMemorySize(availableBlocks * blockSize);
  }
   
  /**
  * 获取SD卡信息
  *
  * @return SDCardInfo
  */
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
  public static String getSDCardInfo() {
  if (!isSDCardEnable()) return null;
  SDCardInfo sd = new SDCardInfo();
  sd.isExist = true;
  StatFs sf = new StatFs(Environment.getExternalStorageDirectory().getPath());
  sd.totalBlocks = sf.getBlockCountLong();
  sd.blockByteSize = sf.getBlockSizeLong();
  sd.availableBlocks = sf.getAvailableBlocksLong();
  sd.availableBytes = sf.getAvailableBytes();
  sd.freeBlocks = sf.getFreeBlocksLong();
  sd.freeBytes = sf.getFreeBytes();
  sd.totalBytes = sf.getTotalBytes();
  return sd.toString();
  }
   
  public static class SDCardInfo {
  boolean isExist;
  long totalBlocks;
  long freeBlocks;
  long availableBlocks;
  long blockByteSize;
  long totalBytes;
  long freeBytes;
  long availableBytes;
   
  @Override
  public String toString() {
  return "isExist=" + isExist +
  "\ntotalBlocks=" + totalBlocks +
  "\nfreeBlocks=" + freeBlocks +
  "\navailableBlocks=" + availableBlocks +
  "\nblockByteSize=" + blockByteSize +
  "\ntotalBytes=" + totalBytes +
  "\nfreeBytes=" + freeBytes +
  "\navailableBytes=" + availableBytes;
  }
  }
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值