android 6.0後讀取外部SD卡的路徑的方法
16lz
2021-01-23
阅读更多
android 中獲取內部SD卡存儲路徑是有標準API的如下:
String internalSDPath = Environment.getExternalStorageDirectory().getAbsolutePath();
外部SD卡路徑的獲取方法如下:
先遍歷掛載的外部設備public ArrayListgetExternalStorageList() { 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"; } }
更多相关文章
- EditText设置更多文字为省略号
- 短视频PHP源码Android 打造专属的下拉刷新 加载更多
- Android 1.6 支持更多的屏幕大小和分辨率
- NestedScrollView+RecyclerView滑动冲突问题,加载更多
- Activity生命周期的回调,你应该知道得更多!--Android源码剖析(上)
- Android中文文档v0.1 beta低调发布,期待更多同学来参加review
- Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公
- Android recyclerview实现查看更多/收起功能
- Android ListView列表 刷新和加载更多