Android 列出当前可用的存储器根目录代码
时间: 2024-02-20 18:00:24 浏览: 28
以下是列出当前可用的存储器根目录的Java代码:
```java
// 判断扩展SD卡存储器是否可用
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File externalSdCardStorage = new File("/mnt/sdcard/external_sd/");
if (externalSdCardStorage.exists() && externalSdCardStorage.isDirectory()) {
Log.d("Storage", "External SD Card Storage: " + externalSdCardStorage);
}
}
// 判断USB存储器是否可用
File usbStorage = new File("/mnt/usb_storage/");
if (usbStorage.exists() && usbStorage.isDirectory()) {
Log.d("Storage", "USB Storage: " + usbStorage);
}
// 判断OTG存储器是否可用
File otgStorage = new File("/mnt/usb_otg/");
if (otgStorage.exists() && otgStorage.isDirectory()) {
Log.d("Storage", "OTG Storage: " + otgStorage);
}
// 判断SD卡存储器是否可用
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File sdCardStorage = Environment.getExternalStorageDirectory();
if (sdCardStorage.exists() && sdCardStorage.isDirectory()) {
Log.d("Storage", "SD Card Storage: " + sdCardStorage);
}
}
// 判断SD卡二级存储器是否可用
File sdCardSecondaryStorage = new File("/mnt/sdcard/external_sd/");
if (sdCardSecondaryStorage.exists() && sdCardSecondaryStorage.isDirectory()) {
Log.d("Storage", "SD Card Secondary Storage: " + sdCardSecondaryStorage);
}
```
以上代码中使用了`Environment.getExternalStorageState()`方法判断SD卡存储器是否可用,使用了`exists()`和`isDirectory()`方法判断其他存储器是否可用。需要注意的是,不同的设备厂商可能会对存储器的挂载点进行修改,因此实际的存储器根目录可能会有所不同。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)