標籤:des android style blog http color java 使用
StorageManager
StorageManager is the interface to the systems storage service. The storage manager handles storage-related items such as Opaque Binary Blobs (OBBs).
OBBs contain a filesystem that maybe be encrypted on disk and mounted on-demand from an application. OBBs are a good way of providing large amounts of binary assets without packaging them into APKs as they may be multiple gigabytes in size. However, due to their size, they‘re most likely stored in a shared storage pool accessible from all programs. The system does not guarantee the security of the OBB file itself: if any program modifies the OBB, there is no guarantee that a read from that OBB will produce the expected output.
Get an instance of this class by calling getSystemService(java.lang.String) with an argument of STORAGE_SERVICE.
從Android 2.3開始新增了一個OBB檔案系統和StorageManager類用來管理外部儲存上的資料安全。
如果我們設計一款資源套件含比較多的遊戲,可能你會發現最終產生的APK檔案可能高達300MB,但是APK檔案很大導致Android系統無法正常安裝,而這麼大其實都是遊戲中用到的資源檔,我們放到SD卡上可能其他應用也可以訪問,比如說系統的圖片管理器會索引遊戲中的圖片資源,而音樂播放器也會索引資源中的音樂,所以Android 2.3的OBB檔案(Opaque Binary Blob)可以很好的解決大檔案在SD卡上分離出APK檔案,同時別的程式沒有許可權訪問這樣一種隔離的檔案系統。
android.os.storage.StorageManager類的執行個體化方法需要使用 getSystemService(Contxt.STORAGE_SERVICE)才可以,eoe再次提醒這是一個API Level至少為9才能調用的類,注意SDK版本以及目標裝置的韌體。
我們知道android上一般都有外置的儲存卡,
但是通過Environment.getExternalStorageDirectory()擷取的是內建的儲存卡位置 (也有的手機可以在系統中修改預設儲存) 那麼如何擷取外置儲存卡的位置呢?
通過StorageManager來擷取多個sdcard,比使用cat /proc/mounts要好:
public static String[] getStoragePaths(Context cxt) { List<String> pathsList = new ArrayList<String>(); if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.GINGERBREAD) { StringBuilder sb = new StringBuilder(); try { pathsList.addAll(new SdCardFetcher().getStoragePaths(new FileReader("/proc/mounts"), sb)); } catch (FileNotFoundException e) { e.printStackTrace(); File externalFolder = Environment.getExternalStorageDirectory(); if (externalFolder != null) { pathsList.add(externalFolder.getAbsolutePath()); } } } else { StorageManager storageManager = (StorageManager) cxt.getSystemService(Context.STORAGE_SERVICE); try { Method method = StorageManager.class.getDeclaredMethod("getVolumePaths"); method.setAccessible(true); Object result = method.invoke(storageManager); if (result != null && result instanceof String[]) { String[] pathes = (String[]) result; StatFs statFs; for (String path : pathes) { if (!TextUtils.isEmpty(path) && new File(path).exists()) { statFs = new StatFs(path); if (statFs.getBlockCount() * statFs.getBlockSize() != 0) { pathsList.add(path); } } } } } catch (Exception e) { e.printStackTrace(); File externalFolder = Environment.getExternalStorageDirectory(); if (externalFolder != null) { pathsList.add(externalFolder.getAbsolutePath()); } } } return pathsList.toArray(new String[pathsList.size()]); }