Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 獲取內外SD卡路徑幾種方法總結

Android 獲取內外SD卡路徑幾種方法總結

編輯:關於Android編程

Android 獲取SD卡路徑:

外置sd卡路徑,也許很多同學在平時的工作中並不會用到,因為現在很多機型都不支持外置sd卡(這也是Google目標),所以並不用考慮外置sd卡的路徑問題。除了開發文件管理類的應用之外,其他應用使用 Enviroment 這個類中的一些靜態方法就能滿足需要。但也有一些特殊需求需要用到外置sd卡路徑,那怎麼才能准確獲得外置sd卡的路徑呢?

方法一

//內置sd卡路徑
String sdcardPath = System.getenv("EXTERNAL_STORAGE"); 
//內置sd卡路徑
String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
//外置置sd卡路徑
String extSdcardPath = System.getenv("SECONDARY_STORAGE");
在Enviroment類的源碼中獲得sd卡路徑其實也是通過 System.getnv() 方法來實現的,如隱藏的方法:

/** {@hide} */
public static File getLegacyExternalStorageDirectory() {
  return new File(System.getenv(ENV_EXTERNAL_STORAGE));
} 

注:更詳細的內容還是去看Enviroment源碼。

另外要注意的是,在API 23版本中 SECONDARY_STORAGE 被移除。

方法二

private static String getStoragePath(Context mContext, boolean is_removale) { 

   StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
    Class<?> storageVolumeClazz = null;
    try {
      storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
      Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
      Method getPath = storageVolumeClazz.getMethod("getPath");
      Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
      Object result = getVolumeList.invoke(mStorageManager);
      final int length = Array.getLength(result);
      for (int i = 0; i < length; i++) {
        Object storageVolumeElement = Array.get(result, i);
        String path = (String) getPath.invoke(storageVolumeElement);
        boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
        if (is_removale == removable) {
          return path;
        }
      }
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }
    return null;
}

通過反射的方式使用在sdk中被 隱藏 的類 StroageVolume 中的方法getVolumeList(),獲取所有的存儲空間(Stroage Volume),然後通過參數is_removable控制,來獲取內部存儲和外部存儲(內外sd卡)的路徑,參數 is_removable為false時得到的是內置sd卡路徑,為true則為外置sd卡路徑。

在API 23 Enviroment 類中的內部類 UserEnvironment 中有一方法getExternalDirs與此一樣,代碼如下:

public File[] getExternalDirs() {
  final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId,StorageManager.FLAG_FOR_WRITE);
  final File[] files = new File[volumes.length];
  for (int i = 0; i < volumes.length; i++) {
    files[i] = volumes[i].getPathFile();
  }
  return files;
}

再看Enviroment的getExternalStorageDirectory方法實現:

public static File getExternalStorageDirectory() {
  throwIfUserRequired();
  return sCurrentUser.getExternalDirs()[0];
}

可以看出,在API 23時,先是通過getExternalDirs()獲取到所有存儲空間的File[]數組,這個數組的第一個值:getExternalDirs()[0],即為內置sd卡所在路徑。

而在API 23 之前的版本中,並沒有類似getExternalDirs()的方法通過StorageVolume直接獲得存儲空間(Storage Volume),而時通過別的方式來實現的,看關鍵方法的源碼:

public static File getExternalStorageDirectory() {
  throwIfUserRequired();
  return sCurrentUser.getExternalDirsForApp()[0];
}

這裡的 getExternalDirsForApp() 和上面的 getExternalDirs() 的作用是一樣的,都是得到所有存儲空間的File[]數組。

public File[] getExternalDirsForApp() {
  return mExternalDirsForApp;
}

mExternalDirsForApp 是在 Enviroment 類中的內部類 UserEnvironment 的構造方法中初始化的,Enviroment#UserEnvironment構造函數源碼如下:

  public UserEnvironment(int userId) {
      // See storage config details at http://source.android.com/tech/storage/
      String rawExternalStorage = System.getenv(ENV_EXTERNAL_STORAGE);
      String rawEmulatedSource = System.getenv(ENV_EMULATED_STORAGE_SOURCE);
      String rawEmulatedTarget = System.getenv(ENV_EMULATED_STORAGE_TARGET);

      String rawMediaStorage = System.getenv(ENV_MEDIA_STORAGE);
      if (TextUtils.isEmpty(rawMediaStorage)) {
        rawMediaStorage = "/data/media";
      }

      ArrayList<File> externalForVold = Lists.newArrayList();
      ArrayList<File> externalForApp = Lists.newArrayList();

      if (!TextUtils.isEmpty(rawEmulatedTarget)) {
        // Device has emulated storage; external storage paths should have
        // userId burned into them.
        final String rawUserId = Integer.toString(userId);
        final File emulatedSourceBase = new File(rawEmulatedSource);
        final File emulatedTargetBase = new File(rawEmulatedTarget);
        final File mediaBase = new File(rawMediaStorage);

        // /storage/emulated/0
        externalForVold.add(buildPath(emulatedSourceBase, rawUserId));
        externalForApp.add(buildPath(emulatedTargetBase, rawUserId));
        // /data/media/0
        mEmulatedDirForDirect = buildPath(mediaBase, rawUserId);

      } else {
        // Device has physical external storage; use plain paths.
        if (TextUtils.isEmpty(rawExternalStorage)) {
          Log.w(TAG, "EXTERNAL_STORAGE undefined; falling back to default");
          rawExternalStorage = "/storage/sdcard0";
        }

        // /storage/sdcard0
        externalForVold.add(new File(rawExternalStorage));
        externalForApp.add(new File(rawExternalStorage));
        // /data/media
        mEmulatedDirForDirect = new File(rawMediaStorage);
      }

      // Splice in any secondary storage paths, but only for owner
      final String rawSecondaryStorage = System.getenv(ENV_SECONDARY_STORAGE);
      if (!TextUtils.isEmpty(rawSecondaryStorage) && userId == UserHandle.USER_OWNER) {
        for (String secondaryPath : rawSecondaryStorage.split(":")) {
          externalForVold.add(new File(secondaryPath));
          externalForApp.add(new File(secondaryPath));
        }
      }

      mExternalDirsForVold = externalForVold.toArray(new File[externalForVold.size()]);
      mExternalDirsForApp = externalForApp.toArray(new File[externalForApp.size()]);
    }

也可以根據這個方法得到一個獲取所有存儲空間的路徑的方法getStorageDirectories():

/**
   * Returns all available SD-Cards in the system (include emulated)
   * <p/>
   * Warning: Hack! Based on Android source code of version 4.3 (API 18)
   * Because there is no standard way to get it.
   * TODO: Test on future Android versions 4.4+
   *
   * @return paths to all available SD-Cards in the system (include emulated)
   */
  private static final Pattern DIR_SEPARATOR = Pattern.compile("/");
  public List<String> getStorageDirectories() {
    // Final set of paths
    final ArrayList<String> rv = new ArrayList<String>();
    // Primary physical SD-CARD (not emulated)
    final String rawExternalStorage = System.getenv("EXTERNAL_STORAGE");
    // All Secondary SD-CARDs (all exclude primary) separated by ":"
    final String rawSecondaryStoragesStr = System.getenv("SECONDARY_STORAGE");
    // Primary emulated SD-CARD
    final String rawEmulatedStorageTarget = System.getenv("EMULATED_STORAGE_TARGET");
    if (TextUtils.isEmpty(rawEmulatedStorageTarget)) {
      // Device has physical external storage; use plain paths.
      if (TextUtils.isEmpty(rawExternalStorage)) {
        // EXTERNAL_STORAGE undefined; falling back to default.
        rv.add("/storage/sdcard0");
      } else {
        rv.add(rawExternalStorage);
      }
    } else {
      // Device has emulated storage; external storage paths should have
      // userId burned into them.
      final String rawUserId;
      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        rawUserId = "";
      } else {
        final String path = Environment.getExternalStorageDirectory().getAbsolutePath();
        final String[] folders = DIR_SEPARATOR.split(path);
        final String lastFolder = folders[folders.length - 1];
        boolean isDigit = false;
        try {
          Integer.valueOf(lastFolder);
          isDigit = true;
        } catch (NumberFormatException ignored) {
        }
        rawUserId = isDigit ? lastFolder : "";
      }
      // /storage/emulated/0[1,2,...]
      if (TextUtils.isEmpty(rawUserId)) {
        rv.add(rawEmulatedStorageTarget);
      } else {
        rv.add(rawEmulatedStorageTarget + File.separator + rawUserId);
      }
    }
    // Add all secondary storages
    if (!TextUtils.isEmpty(rawSecondaryStoragesStr)) {
      // All Secondary SD-CARDs splited into array
      final String[] rawSecondaryStorages = rawSecondaryStoragesStr.split(File.pathSeparator);
      Collections.addAll(rv, rawSecondaryStorages);
    }
    rootmode = Sp.getBoolean("rootmode", false);
    if (rootmode)
      rv.add("/");
    File usb = getUsbDrive();
    if (usb != null && !rv.contains(usb.getPath())) rv.add(usb.getPath());

    return rv;
  }
public File getUsbDrive() {
    File parent;
    parent = new File("/storage");

    try {
      for (File f : parent.listFiles()) {
        if (f.exists() && f.getName().toLowerCase().contains("usb") && f.canExecute()) {
          return f;
        }
      }
    } catch (Exception e) {
    }
    parent = new File("/mnt/sdcard/usbStorage");
    if (parent.exists() && parent.canExecute())
      return (parent);
    parent = new File("/mnt/sdcard/usb_storage");
    if (parent.exists() && parent.canExecute())
      return parent;

    return null;
  }

綜上分析,通過方法一和方法二都可以正確的獲取內外sd卡路徑,但方法一會存在以下問題:

1、API>=23 時方法一無效(暫未測試)

2、有些廠商的Rom改動太多,對相關原生API的支持存在問題,這時方法一可能會存在問題。

3、其他一些情況造成的原因(基本與2差不多,是ROM等因素造成的)

所以,在使用時建議使用方法二來獲取內外置sd卡路徑,在API 23(Android 6.0)之前使用getStorageDirectories() 應該也是OK的。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved