有用的方法

来源:互联网 发布:ubuntu sd卡挂载 编辑:程序博客网 时间:2024/06/06 21:45
/**     * 获取内置SD卡路径     *     */    public static String getPrimaryStoragePath() {        try {            StorageManager sm = (StorageManager) mContext.getSystemService(Context                .STORAGE_SERVICE);            Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", null);            String[] paths = (String[]) getVolumePathsMethod.invoke(sm, null);            // first element in paths[] is primary storage path            return paths[0];        } catch (Exception e) {            Log.e(TAG, "getPrimaryStoragePath() failed", e);        }        return null;    }
/**     * 获取外置SD卡路径     *     */    public static String getSecondaryStoragePath() {        try {            StorageManager sm = (StorageManager) mContext.getSystemService(Context                .STORAGE_SERVICE);            Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", null);            String[] paths = (String[]) getVolumePathsMethod.invoke(sm, null);            // second element in paths[] is secondary storage path            return paths[1];        } catch (Exception e) {            Log.e(TAG, "getSecondaryStoragePath() failed", e);        }        return null;    }
  public static String getStorageState(String path) {        try {            StorageManager sm = (StorageManager) mContext.getSystemService(Context                .STORAGE_SERVICE);            Method getVolumeStateMethod = StorageManager.class.getMethod("getVolumeState", new Class[] {String.class});            String state = (String) getVolumeStateMethod.invoke(sm, path);            return state;        } catch (Exception e) {            Log.e(TAG, "getStorageState() failed", e);        }        return null;    }/**     * 判断外置sd卡是否存在     *     */    private static boolean isExternalStorageMounted() {          StorageManager sm = (StorageManager) mContext.getSystemService(Context                .STORAGE_SERVICE);        final StorageVolume[] volumes = sm.getVolumeList();          for (StorageVolume v : volumes) {              if (v.isRemovable()) {                  if (Environment.MEDIA_MOUNTED.equals(Environment.getStorageState(v.getPathFile())))                      return true;              }          }          return false;      }  
0 0
原创粉丝点击