編輯:關於Android編程
android系統提供了Environment.getExternalStorageDirectory()接口獲得存儲器的路徑,但是這個接口往往給的結果並不是我們想要的,在某些設備上它返回的是手機內部存儲,某些設備它返回的手機外部存儲。還有就是某些Android設備支持擴展多個sdcard,這個時候想要獲得所有存儲器的掛載路徑,這個接口是沒有辦法辦到的。
只返回手機存儲目錄調用這個方法 實測可用
public File getStorage(){ StorageManager manager =(StorageManager) getSystemService(STORAGE_SERVICE); try { Class[] paramClasses = {}; Method getVolumeList; getVolumeList = StorageManager.class.getMethod("getVolumeList", paramClasses); getVolumeList.setAccessible(true); Object[] params = {}; Object[] invokes = (Object[]) getVolumeList.invoke(manager, params); if (invokes != 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]); File file = new File(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(manager, path); } catch (Exception e) { e.printStackTrace(); } boolean canRemovable = ((Boolean) isRemovable.invoke(obj, new Object[0])).booleanValue(); //不可刪除的並且掛載的是手機存儲 if (!canRemovable&&state.equals(Environment.MEDIA_MOUNTED)) { return file ; } } } } } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 publicclassStorageInfo{ publicStringpath; publicStringstate; publicbooleanisRemoveable; publicStorageInfo(Stringpath){ this.path=path; } publicbooleanisMounted(){ return"mounted".equals(state); } }
public static ListlistAvaliableStorage(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; }
1 MethodisRemovable=obj.getClass().getMethod("isRemovable",newClass[0]);
1 2 3 MethodgetVolumeState=StorageManager.class.getMethod("getVolumeState",String.class); state=(String)getVolumeState.invoke(storageManager,info.path); info.state=state;
先對上一遍的工具類,補充兩點:1、Client關閉異常如果沒有連接host就調用close()的話,會導致NullPointException,因為mInputStrea
今天工作中遇到的一個問題,需求是這樣的,需要給dz的論壇做一個android擴展,這肯定少不了會員登錄,就得需要二次開發dz提供一個登錄接口,眩暈中。。。 因為我對dz不
SearchView是搜索框組件,它可以讓用戶在文本框裡輸入文字,通過監聽器取得用戶的輸入,當用戶點擊搜索時,監聽器執行實際的搜索。本文就為大家分享了SearchView
說來說去都不如 畫圖示意 簡單易懂啊!!!真是的! 來吧~~先上張圖~~! (一)首先明確一下Android中的坐標系統:屏幕的左上角是坐標系統原點(0,0)原