/** Get SD card info detail. */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public static SDCardInfo getSDCardInfo() { SDCardInfo sd = new SDCardInfo(); String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { sd.isExist = true; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { File sdcardDir = Environment.getExternalStorageDirectory(); StatFs sf = new StatFs(sdcardDir.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(); } } if (Log.isPrint) { Log.i(TAG, sd.toString()); } return sd; }
/** Get SD card path by CMD. */ public static String getSDCardPath() { String cmd = "cat /proc/mounts"; String sdcard = null; Runtime run = Runtime.getRuntime(); // 返回与当前 Java 应用程序相关的运行时对象 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) { Log.i(TAG, "proc/mounts: " + lineStr); if (lineStr.contains("sdcard") && lineStr.contains(".android_secure")) { String[] strArray = lineStr.split(" "); if (strArray.length >= 5) { sdcard = strArray[1].replace("/.android_secure", ""); Log.i(TAG, "find sd card path: " + sdcard); return sdcard; } } if (p.waitFor() != 0 && p.exitValue() == 1) { // p.exitValue()==0表示正常结束,1:非正常结束 Log.e(TAG, cmd + " 命令执行失败"); } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (bufferedReader != null) { bufferedReader.close(); } } catch (IOException e) { e.printStackTrace(); } } sdcard = Environment.getExternalStorageDirectory().getPath(); Log.i(TAG, "not find sd card path return default: " + sdcard); return sdcard; }
public static Object get(Context context, String fileName, String key, Cipher cipher) { try { String hex = get(context, fileName, key, (String) null); if (hex == null) return null; byte[] bytes = HexUtil.decodeHex(hex.toCharArray()); if (cipher != null) bytes = cipher.decrypt(bytes); Object obj = ByteUtil.byteToObject(bytes); Log.i(TAG, key + " get: " + obj); return obj; } catch (Exception e) { e.printStackTrace(); } return null; }
public static void put(Context context, String fileName, String key, Object ser, Cipher cipher) { SharedPreferences sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE); try { Log.i(TAG, key + " put: " + ser); if (ser == null) { sp.edit().remove(key).commit(); } else { byte[] bytes = ByteUtil.objectToByte(ser); if (cipher != null) bytes = cipher.encrypt(bytes); put(context, fileName, key, HexUtil.encodeHexStr(bytes)); } } catch (Exception e) { e.printStackTrace(); } }
/** Get SD card path list. */ public static ArrayList<String> getSDCardPathEx() { ArrayList<String> list = new ArrayList<String>(); try { Runtime runtime = Runtime.getRuntime(); Process proc = runtime.exec("mount"); InputStream is = proc.getInputStream(); InputStreamReader isr = new InputStreamReader(is); String line; BufferedReader br = new BufferedReader(isr); while ((line = br.readLine()) != null) { Log.i(TAG, "mount: " + line); if (line.contains("secure")) { continue; } if (line.contains("asec")) { continue; } if (line.contains("fat")) { String columns[] = line.split(" "); if (columns.length > 1) { list.add("*" + columns[1]); } } else if (line.contains("fuse")) { String columns[] = line.split(" "); if (columns.length > 1) { list.add(columns[1]); } } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return list; }