因为不一样的厂家定义的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)); } }