TF卡测试

    由于不同的厂家定义的TF卡路径不一,并且有些TF卡不支持热插拔,在盒子上进行TF卡测试就成了一项比较烦躁的任务,针对这些这里简要介绍一种TF卡测试的方法,也可以运用到USB测试中,不过USB是支持热插拔的,相比较而言简单得多。话不多说,先上代码:

public class ExternalStorageUtils {
 private static final String TAG = "ExternalStorageUtils";
 /**
  * 获取扩展存储路径,TF卡、U盘
  */
 public static String getExternalStorageDirectory() {
  String dir = new String();
  InputStream is = null;
  InputStreamReader isr = null;
  BufferedReader br = null;
  try {
   Runtime runtime = Runtime.getRuntime();
   Process proc = runtime.exec("mount");
   is = proc.getInputStream();
   isr = new InputStreamReader(is);
   String line;
   br = new BufferedReader(isr);
   while ((line = br.readLine()) != null) {
    if (line.contains("secure"))
     continue;
    if (line.contains("asec"))
     continue;
    if (line.contains("fat")) {
     String columns[] = line.split(" ");
     if (columns != null && columns.length > 1) {
      dir = dir.concat(columns[1] + "\n");
     }
    } else if (line.contains("fuse")) {
     String columns[] = line.split(" ");
     if (columns != null && columns.length > 1) {
      dir = dir.concat(columns[1] + "\n");
     }
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
   Log.e(TAG, "error get directory");
   return null;
  } finally {
   try {
    if (br != null)
     br.close();
    if (isr != null)
     isr.close();
    if (is != null)
     is.close();
   } catch (Exception e) {
    e.printStackTrace();
    return null;
   }
  }
  return dir;
 }
 /**
  * 获取存储卡存储空间
  * 
  * @param path
  * @return
  */
 public static String getExternalStorageSpace(String path) {
  File file = new File(path);
  Log.i(TAG, "file =" + file.getPath() + ";file exit:" + file.exists());
  StatFs statFs = null;
  String space = null;
  try {
   statFs = new StatFs(file.getPath());
   int blockSize = statFs.getBlockSize();
   int totalBlockNumber = statFs.getBlockCount();
   int availableBlockNumber = statFs.getAvailableBlocks();
   int totalSpaceMB = blockSize / 1024 * totalBlockNumber / 1024;
   int avaSpaceMB = blockSize / 1024 * availableBlockNumber / 1024;
   Log.i(TAG, "ExternalStorage blockSize =" + blockSize);
   Log.i(TAG, "ExternalStorage totalBlockNumber =" + totalBlockNumber);
   Log.i(TAG, "ExternalStorage availableBlockNumber =" + availableBlockNumber);
   Log.i(TAG, "ExternalStorage totalSpace =" + totalSpaceMB + "/MB");
   Log.i(TAG, "ExternalStorage availableSpare =" + avaSpaceMB + "/MB");
   space = avaSpaceMB + "/" + totalSpaceMB + "(MB)";
  } catch (Exception e) {
   e.printStackTrace();
   Log.e(TAG, "error path");
   return null;
  }
  return space;
 }
}

       这是一个获取外部存储设备的工具类,主要有两个功能一个是获取外部存储设备的路径,一个是获取外部存储设备的空间信息。

      下面是TF卡测试的具体方法实现:

// TF测试
 private void tfTest() {
  Log.i(TAG, "tfTest...s");
  String tfPath = ExternalStorageUtils.getExternalStorageDirectory();
  Log.i(TAG, "tfPath:" + tfPath);
  if (tfPath != null && !tfPath.equals("")) {
   if (tfPath.contains(Const.TF_PATH)) {//和已知TF卡路径对比是否存在
    String space = ExternalStorageUtils.getExternalStorageSpace(Const.TF_PATH);
    showToast(mContext.getString(R.string.tf_space) + space);//存在获取TF卡容量信息并显示
       } else {
    Log.e(TAG, " is not tfPath");
    showToast(mContext.getString(R.string.untested_tf));
       }
  } else {
   Log.e(TAG, "tfPath is not exist");
   showToast(mContext.getString(R.string.untested_tf));
    }
 }

转载于:https://my.oschina.net/u/1999544/blog/354928

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值