您好,登錄后才能下訂單哦!
android系統(tǒng)提供了Environment.getExternalStorageDirectory()接口獲得存儲(chǔ)器的路徑,但是這個(gè)接口往往給的結(jié)果并不是我們想要的,在某些設(shè)備上它返回的是手機(jī)內(nèi)部存儲(chǔ),某些設(shè)備它返回的手機(jī)外部存儲(chǔ)。還有就是某些Android設(shè)備支持?jǐn)U展多個(gè)sdcard,這個(gè)時(shí)候想要獲得所有存儲(chǔ)器的掛載路徑,這個(gè)接口是沒有辦法辦到的。
怎么獲取Android設(shè)備所有存儲(chǔ)器的位置呢?或者說獲得所有的掛載點(diǎn)
系統(tǒng)提供了一個(gè)StorageManager,它有一個(gè)方法叫g(shù)etVolumeList,這個(gè)方法的返回值是一個(gè)StorageVolume數(shù)組,StorageVolume類中封裝了掛載路徑,掛載狀態(tài),以及是否可以移除等等信息。但是很可惜,這個(gè)方法是隱藏的api,所以我們只能通過反射來調(diào)用這個(gè)方法了,下面是這個(gè)方法的源碼。
public StorageVolume[] getVolumeList() { if (mMountService == null) return new StorageVolume[0]; try { Parcelable[] list = mMountService.getVolumeList(); if (list == null) return new StorageVolume[0]; int length = list.length; StorageVolume[] result = new StorageVolume[length]; for (int i = 0; i < length; i++) { result[i] = (StorageVolume)list[i]; } return result; } catch (RemoteException e) { Log.e(TAG, "Failed to get volume list", e); return null; } }
通過反射,獲取到Android設(shè)備所有存儲(chǔ)器。
public class StorageInfo { public String path; public String state; public boolean isRemoveable; public StorageInfo(String path) { this.path = path; } public boolean isMounted() { return "mounted".equals(state); } }
public static List listAvaliableStorage(Context context) { ArrayList storagges = new ArrayList(); StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); try { Class<?>[] paramClasses = {}; Method getVolumeList = StorageManager.class.getMethod("getVolumeList", paramClasses); getVolumeList.setAccessible(true); Object[] params = {}; Object[] invokes = (Object[]) getVolumeList.invoke(storageManager, params); if (invokes != null) { StorageInfo info = null; for (int i = 0; i < invokes.length; i++) { Object obj = invokes[i]; Method getPath = obj.getClass().getMethod("getPath", new Class[0]); String path = (String) getPath.invoke(obj, new Object[0]); info = new StorageInfo(path); File file = new File(info.path); if ((file.exists()) && (file.isDirectory()) && (file.canWrite())) { Method isRemovable = obj.getClass().getMethod("isRemovable", new Class[0]); String state = null; try { Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class); state = (String) getVolumeState.invoke(storageManager, info.path); info.state = state; } catch (Exception e) { e.printStackTrace(); } if (info.isMounted()) { info.isRemoveable = ((Boolean) isRemovable.invoke(obj, new Object[0])).booleanValue(); storagges.add(info); } } } } } catch (NoSuchMethodException e1) { e1.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } storagges.trimToSize(); return storagges; }
如何判斷存儲(chǔ)器是內(nèi)置存儲(chǔ)還是外置存儲(chǔ)呢?
StorageVolume這個(gè)類中提供了一個(gè)isRemovable()接口,通過反射調(diào)用它就可以知道存儲(chǔ)器是否可以移除。把可以移除的存儲(chǔ)器認(rèn)定為外置sdcard,不可移除的存儲(chǔ)器認(rèn)定為內(nèi)置存儲(chǔ)器。
Method isRemovable = obj.getClass().getMethod("isRemovable", new Class[0]);
如何判斷存儲(chǔ)器的掛載狀態(tài)呢?
同上面一樣,需要反射系統(tǒng)接口才可以獲取到掛載狀態(tài)。下面是代碼片段
Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class); state = (String) getVolumeState.invoke(storageManager, info.path); info.state = state;
總結(jié)
通過反射系統(tǒng)的StorageManager以及StorageVolume類提供的接口,就可以拿到Android設(shè)備掛載的所有存儲(chǔ)器路徑,以及存儲(chǔ)器類型(內(nèi)置存儲(chǔ)還是外置存儲(chǔ)),還有存儲(chǔ)器的掛載狀態(tài)等信息。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。