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è)接口是沒(méi)有辦法辦到的。

創(chuàng)新互聯(lián)建站專注于網(wǎng)站建設(shè)|成都網(wǎng)站維護(hù)公司|優(yōu)化|托管以及網(wǎng)絡(luò)推廣,積累了大量的網(wǎng)站設(shè)計(jì)與制作經(jīng)驗(yàn),為許多企業(yè)提供了網(wǎng)站定制設(shè)計(jì)服務(wù),案例作品覆蓋成都房屋鑒定等行業(yè)。能根據(jù)企業(yè)所處的行業(yè)與銷售的產(chǎn)品,結(jié)合品牌形象的塑造,量身定制品質(zhì)網(wǎng)站。
怎么獲取Android設(shè)備所有存儲(chǔ)器的位置呢?或者說(shuō)獲得所有的掛載點(diǎn)
系統(tǒng)提供了一個(gè)StorageManager,它有一個(gè)方法叫g(shù)etVolumeList,這個(gè)方法的返回值是一個(gè)StorageVolume數(shù)組,StorageVolume類中封裝了掛載路徑,掛載狀態(tài),以及是否可以移除等等信息。但是很可惜,這個(gè)方法是隱藏的api,所以我們只能通過(guò)反射來(lái)調(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;
}
}
通過(guò)反射,獲取到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()接口,通過(guò)反射調(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é)
通過(guò)反射系統(tǒng)的StorageManager以及StorageVolume類提供的接口,就可以拿到Android設(shè)備掛載的所有存儲(chǔ)器路徑,以及存儲(chǔ)器類型(內(nèi)置存儲(chǔ)還是外置存儲(chǔ)),還有存儲(chǔ)器的掛載狀態(tài)等信息。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前題目:如何獲取Android設(shè)備掛載的所有存儲(chǔ)器
標(biāo)題URL:http://www.yijiale78.com/article28/jjpojp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站維護(hù)、商城網(wǎng)站、做網(wǎng)站、品牌網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)