新聞中心
怎么在Android中獲得所有存儲(chǔ)設(shè)備位置?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)是專業(yè)的湯原網(wǎng)站建設(shè)公司,湯原接單;提供網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行湯原網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
具體代碼如下:
public static ListgetAllExternalStorage(Context context) { List storagePath = new ArrayList<>(); StorageManager storageManager = (StorageManager) context.getSystemService(STORAGE_SERVICE); StorageVolume[] storageVolumes; try { Method getVolumeList = StorageManager.class.getDeclaredMethod("getVolumeList"); storageVolumes = (StorageVolume[]) getVolumeList.invoke(storageManager); Method getVolumeState = StorageManager.class.getDeclaredMethod("getVolumeState", String.class); for (StorageVolume storageVolume : storageVolumes) { String desc = storageVolume.getDescription(context); Log.i(TAG, "storageVolume name--->" + desc); Method getPath = StorageVolume.class.getMethod("getPath"); String path = (String) getPath.invoke(storageVolume); Log.i(TAG, "StoragePath--->" + path); //這里需要用StorageManager反射調(diào)用getVolumeState函數(shù),而不應(yīng)該用StorageVolume的getState方法,因?yàn)榭赡軙?huì)報(bào)錯(cuò) String state = (String) getVolumeState.invoke(storageManager, path); Log.i(TAG, "storageVolume State--->" + state); if (Environment.MEDIA_MOUNTED.equals(state)) { HomeDirBean bean = new HomeDirBean(path, desc); storagePath.add(bean); } } } catch (Exception e) { Log.e(TAG, e.getMessage()); } return storagePath; }
這里需要注意,可能有小伙伴會(huì)問,既然StorageVolume類有g(shù)etState方法,為啥還要用StorageManager反射調(diào)用getVolumeState方法,并傳入path地址,而在源碼里,StorageManager的getVolumeState的方法的實(shí)現(xiàn),也是將path重新創(chuàng)建為StorageVolume類,然后再調(diào)用其getState方法,我們這樣做成這不是多此一舉嗎?
源碼截圖如下:
Android是什么
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
名稱欄目:怎么在Android中獲得所有存儲(chǔ)設(shè)備位置
URL鏈接:http://fisionsoft.com.cn/article/ghccch.html