android 6.0後讀取外部SD卡的路徑的方法

android 中獲取內部SD卡存儲路徑是有標準API的如下:

String internalSDPath = Environment.getExternalStorageDirectory().getAbsolutePath();

 

外部SD卡路徑的獲取方法如下:

先遍歷掛載的外部設備
public ArrayList<String> getExternalStorageList() {
        ArrayList storageList = new ArrayList();

        try {
            StorageManager mStorageManager = (StorageManager)getSystemService("storage");

            try {
                Method mMethodGetPaths = mStorageManager.getClass().getMethod("getVolumeList");
                Object[] list = (Object[])((Object[])mMethodGetPaths.invoke(mStorageManager));
                if (list != null && list.length > 0) {
                    Object[] arr$ = list;
                    int len$ = list.length;

                    for(int i$ = 0; i$ < len$; ++i$) {
                        Object item = arr$[i$];
                        StorageVolumeReflection storageVolume = new StorageVolumeReflection(this, item);
                        Log.i("TEST", "storageVolume = " + storageVolume);
                        if (!storageVolume.mPrimary && storageVolume.mRemovable && storageVolume.mState.equals("mounted")) {
                            storageList.add(storageVolume.mPath);
                        }
                    }
                }
            } catch (Exception var10) {
                var10.printStackTrace();
            }
        } catch (Exception var11) {
            var11.printStackTrace();
        }

        return storageList;
    }


外部SD卡的路徑:

ArrayList<String> sdcardPath;
sdcardPath = getExternalStorageList();
Log.i("TEST", "------------>sdcardPath size: " + sdcardPath.size());
if(sdcardPath.size()>0) {
      Log.i("TEST", "------------>sdcardPath: " + sdcardPath.get(0));
}

 創建StorageVolumeReflection 類:

public class StorageVolumeReflection {
        public int mStorageId;
        public String mPath;
        public String mDescription;
        public boolean mPrimary;
        public boolean mRemovable;
        public boolean mEmulated;
        public int mMtpReserveSpace;
        public boolean mAllowMassStorage;
        public long mMaxFileSize;
        public String mState;

        public StorageVolumeReflection(Context context, Object reflectItem) {
            Method fState;
            try {
                fState = reflectItem.getClass().getDeclaredMethod("getStorageId");
                fState.setAccessible(true);
                this.mStorageId = (Integer)fState.invoke(reflectItem);
            } catch (Exception var16) {
                ;
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("getPath");
                fState.setAccessible(true);
                this.mPath = (String)fState.invoke(reflectItem);
            } catch (Exception var15) {
                ;
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("getDescription");
                fState.setAccessible(true);
                this.mDescription = (String)fState.invoke(reflectItem);
            } catch (Exception var14) {
                ;
            }

            if (this.mDescription == null || TextUtils.isEmpty(this.mDescription)) {
                try {
                    fState = reflectItem.getClass().getDeclaredMethod("getDescription");
                    fState.setAccessible(true);
                    this.mDescription = (String)fState.invoke(reflectItem, context);
                } catch (Exception var13) {
                    ;
                }
            }

            if (this.mDescription == null || TextUtils.isEmpty(this.mDescription)) {
                try {
                    fState = reflectItem.getClass().getDeclaredMethod("getDescriptionId");
                    fState.setAccessible(true);
                    int mDescriptionId = (Integer)fState.invoke(reflectItem);
                    if (mDescriptionId != 0) {
                        this.mDescription = context.getResources().getString(mDescriptionId);
                    }
                } catch (Exception var12) {
                    ;
                }
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("isPrimary");
                fState.setAccessible(true);
                this.mPrimary = (Boolean)fState.invoke(reflectItem);
            } catch (Exception var11) {
                ;
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("isRemovable");
                fState.setAccessible(true);
                this.mRemovable = (Boolean)fState.invoke(reflectItem);
            } catch (Exception var10) {
                ;
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("isEmulated");
                fState.setAccessible(true);
                this.mEmulated = (Boolean)fState.invoke(reflectItem);
            } catch (Exception var9) {
                ;
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("getMtpReserveSpace");
                fState.setAccessible(true);
                this.mMtpReserveSpace = (Integer)fState.invoke(reflectItem);
            } catch (Exception var8) {
                ;
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("allowMassStorage");
                fState.setAccessible(true);
                this.mAllowMassStorage = (Boolean)fState.invoke(reflectItem);
            } catch (Exception var7) {
                ;
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("getMaxFileSize");
                fState.setAccessible(true);
                this.mMaxFileSize = (Long)fState.invoke(reflectItem);
            } catch (Exception var6) {
                ;
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("getState");
                fState.setAccessible(true);
                this.mState = (String)fState.invoke(reflectItem);
            } catch (Exception var5) {
                ;
            }

        }

        public String getVolumeState(Context context) {
            return "";
        }

        public boolean isMounted(Context context) {
            return this.getVolumeState(context).equals("mounted");
        }

        public String getDescription() {
            return this.mDescription;
        }

        public String getUniqueFlag() {
            return "" + this.mStorageId;
        }

        public long getAvailableSize() {
            return 0L;
        }

        public long getTotalSize() {
            return 0L;
        }

        public String toString() {
            return "MyStorageVolume{\nmStorageId=" + this.mStorageId + "\n, mPath='" + this.mPath + '\'' + "\n, mDescription=" + this.mDescription + "\n, mPrimary=" + this.mPrimary + "\n, mRemovable=" + this.mRemovable + "\n, mEmulated=" + this.mEmulated + "\n, mMtpReserveSpace=" + this.mMtpReserveSpace + "\n, mAllowMassStorage=" + this.mAllowMassStorage + "\n, mMaxFileSize=" + this.mMaxFileSize + "\n, mState='" + this.mState + '\'' + '}' + "\n";
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章