TF卡測試

    因爲不一樣的廠家定義的TF卡路徑不一,而且有些TF卡不支持熱插拔,在盒子上進行TF卡測試就成了一項比較煩躁的任務,針對這些這裏簡要介紹一種TF卡測試的方法,也能夠運用到USB測試中,不過USB是支持熱插拔的,相比較而言簡單得多。話很少說,先上代碼:java

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));
    }
 }
相關文章
相關標籤/搜索