android u盘检测工具,android U盘检测及获取内存储器信息
时间: 2023-11-13 14:04:17 浏览: 80
您可以尝试使用以下代码来检测和获取Android设备中连接的U盘信息:
```
// 检测U盘是否连接
public boolean isUSBExist(Context context) {
StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
if (storageManager == null) {
return false;
}
try {
Method method = storageManager.getClass().getMethod("getVolumeList");
if (method == null) {
return false;
}
Object[] objArr = (Object[]) method.invoke(storageManager);
if (objArr == null) {
return false;
}
for (Object obj : objArr) {
String path = (String) obj.getClass().getMethod("getPath").invoke(obj);
String state = (String) obj.getClass().getMethod("getState").invoke(obj);
if (Environment.MEDIA_MOUNTED.equals(state) && path != null && path.contains("usb")) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
// 获取U盘信息
public String getUSBInfo(Context context) {
StringBuilder sb = new StringBuilder();
StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
if (storageManager == null) {
return sb.toString();
}
try {
Method method = storageManager.getClass().getMethod("getVolumeList");
if (method == null) {
return sb.toString();
}
Object[] objArr = (Object[]) method.invoke(storageManager);
if (objArr == null) {
return sb.toString();
}
for (Object obj : objArr) {
String path = (String) obj.getClass().getMethod("getPath").invoke(obj);
String state = (String) obj.getClass().getMethod("getState").invoke(obj);
if (Environment.MEDIA_MOUNTED.equals(state) && path != null && path.contains("usb")) {
String description = (String) obj.getClass().getMethod("getDescription", Context.class).invoke(obj, context);
String uuid = (String) obj.getClass().getMethod("getUuid").invoke(obj);
long totalSize = (long) obj.getClass().getMethod("getTotalSize").invoke(obj);
long freeSize = (long) obj.getClass().getMethod("getFreeSize").invoke(obj);
sb.append("Description: ").append(description).append("\n");
sb.append("UUID: ").append(uuid).append("\n");
sb.append("TotalSize: ").append(totalSize).append("B\n");
sb.append("FreeSize: ").append(freeSize).append("B\n");
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
```
以上代码可以通过StorageManager类来获取连接的存储设备信息,包括U盘。其中,isUSBExist方法用于检测U盘是否连接,getUSBInfo方法用于获取U盘的详细信息,包括描述、UUID、总大小和可用大小。
阅读全文