안드로이드 4.0 이상 버전에서 외장 SD 카드의 경로를 어떻게 찾나요?
조회수 3048회
Samsung Galaxy S3는 외장 SD 카드 슬롯이 있는데요, /mnt/extSdCard 경로로 장착됩니다.
Environment.getExternalStorageDirectory() 다음과 같이 코드를 통해서 어떻게 경로를 찾을 수 있을까요? 위 코드는 mnt/sdcard 를 반환할 것입니다. 근데 외장 SD 카드 API는 찾을 수가 없네요.(혹은 테블릿 용 탈부착이 가능한 USB)
감사합니다!
1 답변
-
여기서 찾은 솔루션을 약간 변경해봤습니다.
public static HashSet<String> getExternalMounts() { final HashSet<String> out = new HashSet<String>(); String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*"; String s = ""; try { final Process process = new ProcessBuilder().command("mount") .redirectErrorStream(true).start(); process.waitFor(); final InputStream is = process.getInputStream(); final byte[] buffer = new byte[1024]; while (is.read(buffer) != -1) { s = s + new String(buffer); } is.close(); } catch (final Exception e) { e.printStackTrace(); } // parse output final String[] lines = s.split("\n"); for (String line : lines) { if (!line.toLowerCase(Locale.US).contains("asec")) { if (line.matches(reg)) { String[] parts = line.split(" "); for (String part : parts) { if (part.startsWith("/")) if (!part.toLowerCase(Locale.US).contains("vold")) out.add(part); } } } } return out; }
오리지널 메소드는 아래 기기에서 테스트했고 작동했습니다.
- Huawei X3 (stock)
- Galaxy S2 (stock)
- Galaxy S3 (stock)
테스트했을 때 정확히 어떤 안드로이드 버전이였는지는 기억나지 않습니다. 수정한 버전의 코드로는 아래 기기에서 테스트했습니다.
- Moto Xoom 4.1.2 (stock)
- Galaxy Nexus (cyanogenmod 10) using an otg cable
- HTC Incredible (cyanogenmod 7.2)는 내장, 외장 경로를 모두 반환했습니다. 이 기기는 약간 이상한 점이 있었는데요. getExternalStorage() 메소드가 sdcard 경로를 반환했기 때문에 내장 경로 사용이 점차 줄었습니다.
그리고 SD 카드를 주 저장장치로 사용하는 단일 저장 기기도 테스트해봤습니다.
- HTC G1 (cyanogenmod 6.1)
- HTC G1 (stock)
- HTC Vision/G2 (stock)
놀랄 것도 없이 모든 기기들이 탈부착 저장장치의 경로를 반환했습니다. 물론 추가적으로 확인해볼 필요가 있겠지만, 지금까지 찾은 자료 중에서는 가장 나은 솔루션이라고 생각합니다.
댓글 입력