android u盘检测工具,android U盘检测及获取内存储器信息
时间: 2023-11-13 16:04:40 浏览: 169
您可以尝试使用以下代码来检测并获取U盘信息:
```
private void detectUSB() {
UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
for (UsbDevice device : deviceList.values()) {
if (device.getProductName().contains("USB")) {
String deviceName = device.getDeviceName();
String deviceSerial = device.getSerialNumber();
// 获取U盘的存储容量信息
File path = new File("/storage/" + deviceSerial);
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSizeLong();
long totalBlocks = stat.getBlockCountLong();
long availableBlocks = stat.getAvailableBlocksLong();
long totalSize = totalBlocks * blockSize / (1024 * 1024);
long availableSize = availableBlocks * blockSize / (1024 * 1024);
Log.d(TAG, "U盘名称:" + deviceName + ",U盘序列号:" + deviceSerial + ",总容量:" + totalSize + "MB,可用容量:" + availableSize + "MB");
}
}
}
```
需要注意的是,需要在AndroidManifest.xml文件中添加以下权限:
```
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
同时,在Android 6.0及以上版本需要动态申请读写权限。
阅读全文