TFカードテスト

3705 ワード

メーカーによって定義された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] + "
");      }     } else if (line.contains("fuse")) {      String columns[] = line.split(" ");      if (columns != null && columns.length > 1) {       dir = dir.concat(columns[1] + "
");      }     }    }   } 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;  } }

これは外部ストレージデバイスを取得するツールクラスで、主に2つの機能があります.1つは外部ストレージデバイスを取得するパスであり、1つは外部ストレージデバイスの空間情報を取得することです.
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));
    }
 }