阅读更多

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

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

 

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

先遍歷掛載的外部設備public ArrayList 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 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";        }    }

 

更多相关文章

  1. Android中ExpandableListView控件基本使用
  2. android SlidingMenu 侧边菜单划出后剩余部分阴影设置
  3. android对json数据的解析
  4. cordova的android notify消息通知插件
  5. 解决Android(安卓)Studio Gradle DSL method not found: 'androi
  6. Funambol android eclipse上的配置及说明
  7. android Handler使用
  8. Android编译问题:Only 64-bit build environments are supported
  9. Android中日志信息的打印方式

随机推荐

  1. AIDL接口描述语言的使用
  2. TCPDUMP for Android(抓TCP数据包工具)
  3. Android(安卓)循环滚动控件ViewFlipper,可
  4. 精选的优秀ROM
  5. android之首选项相关 Preferences(三)EditT
  6. kotlin 配置开发环境
  7. Android学习笔记(三)UI布局
  8. Android获取桌面启动图标的位置
  9. Android跨进程通信之ContentProvider
  10. android异常:Can not perform this action