Đây là lớp tôi sử dụng để tìm tất cả thẻ SD trên thiết bị; được tích hợp và tháo lắp. Tôi đã sử dụng nó trên Ice Cream Sandwich, nhưng nó sẽ hoạt động ở mức 2x.
public class GetRemovableDevice {
private final static String TAG = "GetRemoveableDevice";
public GetRemovableDevice() {
}
public static String[] getDirectories() {
MyLog.d(TAG, "getStorageDirectories");
File tempFile;
String[] directories = null;
String[] splits;
ArrayList<String> arrayList = new ArrayList<String>();
BufferedReader bufferedReader = null;
String lineRead;
try {
arrayList.clear(); // redundant, but what the hey
bufferedReader = new BufferedReader(new FileReader("/proc/mounts"));
while ((lineRead = bufferedReader.readLine()) != null) {
MyLog.d(TAG, "lineRead: " + lineRead);
splits = lineRead.split(" ");
// System external storage
if (splits[1].equals(Environment.getExternalStorageDirectory()
.getPath())) {
arrayList.add(splits[1]);
MyLog.d(TAG, "gesd split 1: " + splits[1]);
continue;
}
// skip if not external storage device
if (!splits[0].contains("/dev/block/")) {
continue;
}
// skip if mtdblock device
if (splits[0].contains("/dev/block/mtdblock")) {
continue;
}
// skip if not in /mnt node
if (!splits[1].contains("/mnt")) {
continue;
}
// skip these names
if (splits[1].contains("/secure")) {
continue;
}
if (splits[1].contains("/mnt/asec")) {
continue;
}
// Eliminate if not a directory or fully accessible
tempFile = new File(splits[1]);
if (!tempFile.exists()) {
continue;
}
if (!tempFile.isDirectory()) {
continue;
}
if (!tempFile.canRead()) {
continue;
}
if (!tempFile.canWrite()) {
continue;
}
// Met all the criteria, assume sdcard
arrayList.add(splits[1]);
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
}
}
}
// Send list back to caller
if (arrayList.size() == 0) {
arrayList.add("sdcard not found");
}
directories = new String[arrayList.size()];
for (int i = 0; i < arrayList.size(); i++) {
directories[i] = arrayList.get(i);
}
return directories;
}
}
Các MyLog.d là một lớp dấu vết mà mở rộng Log.d - nó có thể bị loại bỏ.
Lớp đọc/proc/mounts/và:
- kiểm tra xem nếu tên đường dẫn là thư mục sdcard nội
- kiểm tra để xem nếu nó là một thiết bị khối
- bỏ qua mtdblock thiết bị
- bỏ qua bất cứ điều gì mà không được gắn kết
- bỏ qua thư mục an toàn và ASEC
- làm cho chắc chắn nó tồn tại, là một thư mục, và đọc/viết có thể truy cập
Nếu tất cả điều này phù hợp, nó giả định rằng bạn có thẻ sd và thêm đường dẫn vào danh sách mảng. Nó trả về một chuỗi các tên đường dẫn.
Để gọi getDirectories chức năng, mã một cái gì đó tương tự như:
String[] sdcardDirectories = GetRemoveableDevice.getDirectories();
Các đường dẫn trở lại có thể được sử dụng để tạo ra một danh sách lựa chọn sử dụng, quét một tập tin, hoặc bất cứ điều gì.
Cuối cùng, đây là hai dòng MyLog.d từ một thử nghiệm giả lập (dòng thứ hai là sdcard emulator):
09-19 15: 57: 12,511: D/GetRemoveableDevice (651): lineRead:/dev/block/mtdblock2/cache yaffs2 rw, nosuid, nodev 0 0
09-19 15: 57: 12.511: D/GetRemoveableDevice (651): lineRead:/dev/block/vold/179: 0/mnt/sdcard vfat rw, dirsync, nosuid, nodev, noexec, uid = 1000, gid = 1015, fmask = 0702, dmask = 0702, allow_utime = 0020, codepage = cp437, iocharset = iso8859-1, tên ngắn = hỗn hợp, utf8, lỗi = remount -ro 0 0
Bất kỳ thứ gì ngoài các phương pháp trên 'Môi trường' đều nằm ngoài giới hạn của SDK và sẽ không đáng tin cậy. – CommonsWare