Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 通過反射獲取設備存儲器路徑列表

編輯:關於 Android 程式設計

Android 設備的碎片化在存儲路徑上體現得尤為明顯。外置 T 卡可能掛在 /mnt/sdcard、/mnt/extsd、/mnt/external_sd 或 /mnt/sdcard2;內置存儲也可能佔用 /mnt/sdcard;Host USB 存儲的路徑更是各家廠商各有講究。

硬編碼任何一個路徑都是危險的。一款希望在各式設備上都能跑的應用,必須在運行期動態枚舉出所有可用存儲,並在其中一個卷空間不足時自動切換到下一個。

遺憾的是,StorageManager 裡那個正好能給出答案的方法 getVolumePaths() 被標記為 @hide,並未開放給普通應用。繞過限制的一條路是反射。

反射調用隱藏接口

package ckl.storage.list;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.os.storage.StorageManager;

public class StorageList {

    private StorageManager mStorageManager;
    private Method mMethodGetPaths;

    public StorageList(Activity activity) {
        if (activity == null) {
            return;
        }
        mStorageManager = (StorageManager) activity.getSystemService(Activity.STORAGE_SERVICE);
        if (mStorageManager == null) {
            return;
        }
        try {
            mMethodGetPaths = mStorageManager.getClass().getMethod("getVolumePaths");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    public String[] getVolumePaths() {
        if (mMethodGetPaths == null) {
            return null;
        }
        try {
            return (String[]) mMethodGetPaths.invoke(mStorageManager);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
}

實現思路很直白:構造時先拿到 StorageManager 實例,再用反射把 getVolumePaths 這個方法對象取出來緩存;使用時直接 invoke。把 Method 對象存成成員變量而不是每次調用都重新查找,可以省下重複的反射開銷。

被反射的那個方法長什麼樣

StorageManager 中相關的實現大致如下:

/** 返回所有可掛載卷的路徑 */
/** @hide */
public String[] getVolumePaths() {
    StorageVolume[] volumes = getVolumeList();
    if (volumes == null) return null;

    int count = volumes.length;
    String[] paths = new String[count];
    for (int i = 0; i < count; i++) {
        paths[i] = volumes[i].getPath();
    }
    return paths;
}

/** 返回所有可掛載卷 */
/** @hide */
public StorageVolume[] getVolumeList() {
    try {
        Parcelable[] list = mMountService.getVolumeList();
        if (list == null) return new StorageVolume[0];

        int length = list.length;
        StorageVolume[] result = new StorageVolume[length];
        for (int i = 0; i < length; i++) {
            result[i] = (StorageVolume) list[i];
        }
        return result;
    } catch (RemoteException e) {
        Log.e(TAG, "Failed to get volume list", e);
        return null;
    }
}

可以看清一層層剝繭的關係:getVolumePaths 只是把 getVolumeList 拿到的 StorageVolume 數組映射成路徑數組,而真正的數據來自底層的 MountService(Binder 對象 IMountService)。StorageManager 本身只是 MountService 在用戶空間的一層代理,同時還承擔 OBB(Opaque Binary Blob)掛載、USB 大容量存儲開關、存儲狀態監聽等職能。

更值得推薦的做法

反射依賴隱藏實現,方法名或簽名一旦在系統升級中改動,代碼就會在運行期靜默失效——這正是它最大的風險。因此在新項目裡,更稳妥的選擇是使用公開 API:

// 返回所有外置存儲目錄,主存儲排在第 0 位
File[] dirs = context.getExternalFilesDirs(null);

// 或枚舉 Environment 已知的標準目錄
File external = Environment.getExternalStorageDirectory();

getExternalFilesDirs 在各廠商 ROM 上都能返回多個卷(內置 + 外置 SD 卡 + USB),足以覆蓋大多數「多存儲切換」的需求,且不需要任何反射與隱藏接口。反射方案更适合在維護老項目、或需要拿到非應用專用目錄時作為補充手段。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved