Android 获取storage存储根目录
//判断是否是OTG设备,异线程循环查询可改造成OTG设备是否已插入 /** * check if volume is USB OTG. * * @return boolean */ private static boolean isUSBOTG(VolumeInfo volumeInfo) { if (volumeInfo == null) { return false; } DiskInfo diskInfo = volumeInfo.getDisk(); if (diskInfo == null) { return false; } String diskID = diskInfo.getId(); if (diskID != null) { // for usb otg, the disk id same as disk:8:x String[] idSplit = diskID.split(":"); if (idSplit != null && idSplit.length == 2) { if (idSplit[1].startsWith("8,")) { Utils.logd(TAG, "this is a usb otg"); return true; } } } return false; } /** * @return String 获取SDcard目录,此SDCARD是系统内部存储目录,非TF卡目录 */ public static String getInternalSdPath() { Utils.logd(TAG + "/Utils", "-->getDefalueInternalSdPath()"); String internalPath = null; StorageManager storageManager = (StorageManager) MyApplication.getInstance() .getApplicationContext().getSystemService(Context.STORAGE_SERVICE); StorageVolume[] volumes = storageManager.getVolumeList(); for (StorageVolume volume : volumes) { String volumePathStr = volume.getPath(); if (Environment.MEDIA_MOUNTED.equalsIgnoreCase(volume.getState())) { Utils.logd(TAG + "/Utils", volumePathStr + " is mounted!"); VolumeInfo volumeInfo = storageManager.findVolumeById(volume.getId()); if (volumeInfo == null || isUSBOTG(volumeInfo)) { continue; } if (volume.isEmulated()) { String viId = volumeInfo.getId(); Utils.logd(TAG + "/Utils", "Is emulated and volumeInfo.getId() : " + viId); // If external sd card, the viId will be like // "emulated:179,130" if (viId.equalsIgnoreCase("emulated") || viId.equalsIgnoreCase("emulated;0")) { internalPath = volumePathStr; break; } } else { DiskInfo diskInfo = volumeInfo.getDisk(); if (diskInfo == null) { continue; } if (diskInfo.isSd()) { continue; } } } else { Utils.logd(TAG + "/Utils", volumePathStr + " is not mounted!"); } } Utils.logd(TAG + "/Utils", "<--getDefalueInternalSdPath() = " + internalPath); return internalPath; } /** * @return String 获取SDcard目录,此SDCARD是可插播的TF卡,如系统目录 /storage/2368-1312 */ // androidos:/storage $ ls // 2368-1312 emulated sdcard0 self //2368-1312是sdcard fsduuid // androidos:/storage $ public static String getExternalSdPath() { Utils.logd(TAG + "/Utils", "-->getDefaultExternalSdPath()"); String externalPath = null; StorageManager storageManager = (StorageManager) MyApplication.getInstance() .getApplicationContext().getSystemService(Context.STORAGE_SERVICE); StorageVolume[] volumes = storageManager.getVolumeList(); for (StorageVolume volume : volumes) { String volumePathStr = volume.getPath(); if (Environment.MEDIA_MOUNTED.equalsIgnoreCase(volume.getState())) { Utils.logd(TAG + "/Utils", volumePathStr + " is mounted!"); VolumeInfo volumeInfo = storageManager.findVolumeById(volume.getId()); if (volumeInfo == null || isUSBOTG(volumeInfo)) { continue; } if (volume.isEmulated()) { String viId = volumeInfo.getId(); Utils.logd(TAG + "/Utils", "Is emulated and volumeInfo.getId() : " + viId); // If external sd card, the viId will be like // "emulated:179,130" if (!viId.equalsIgnoreCase("emulated") && !viId.equalsIgnoreCase("emulated;0")) { externalPath = volumePathStr; break; } } else { DiskInfo diskInfo = volumeInfo.getDisk(); if (diskInfo == null) { continue; } if (diskInfo.isSd()) { externalPath = volumePathStr; break; } } } else { Utils.logd(TAG + "/Utils", volumePathStr + " is not mounted!"); } } Utils.logd(TAG + "/Utils", "<--getDefaultExternalSdPath() = " + externalPath); return externalPath; }
版权声明:如无特殊标注,文章均为本站原创,转载时请以链接形式注明文章出处。
评论