Skip to content

Instantly share code, notes, and snippets.

@maxlord
Created November 10, 2015 13:20
Show Gist options
  • Save maxlord/6df9813633dc6d15fbed to your computer and use it in GitHub Desktop.
Save maxlord/6df9813633dc6d15fbed to your computer and use it in GitHub Desktop.
Получение пути к флешке Android
String sdCardPath = null;
if (storageState.equals(Environment.MEDIA_MOUNTED)) {
sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
} else {
List<String> possiblePaths = new ArrayList<String>();
possiblePaths.add("/storage/sdcard1 "); //!< Motorola Xoom
possiblePaths.add("/storage/extsdcard "); //!< Samsung SGS3
possiblePaths.add("/storage/sdcard0/external_sdcard"); // user request
possiblePaths.add("/mnt/extsdcard");
possiblePaths.add("/mnt/sdcard/external_sd"); //!< Samsung galaxy family
possiblePaths.add("/mnt/external_sd");
possiblePaths.add("/mnt/media_rw/sdcard1"); //!< 4.4.2 on CyanogenMod S3
possiblePaths.add("/removable/microsd"); //!< Asus transformer prime
possiblePaths.add("/mnt/emmc");
possiblePaths.add("/storage/external_SD"); //!< LG
possiblePaths.add("/storage/ext_sd"); //!< HTC One Max
possiblePaths.add("/storage/removable/sdcard1"); //!< Sony Xperia Z1
possiblePaths.add("/data/sdext");
possiblePaths.add("/data/sdext2");
possiblePaths.add("/data/sdext3");
possiblePaths.add("/data/sdext4");
for (String path : possiblePaths) {
File f = new File(path);
if (f.isDirectory() && f.canWrite()) {
sdCardPath = f.getAbsolutePath();
break;
}
}
if (sdCardPath == null) {
// читаем файл /proc/mounts
try {
File mountFile = new File("/proc/mounts");
if(mountFile.exists()){
Scanner scanner = new Scanner(mountFile);
while (scanner.hasNext()) {
String line = scanner.nextLine();
if (line.startsWith("/dev/block/vold/")) {
String[] lineElements = line.split(" ");
String element = lineElements[1];
sdCardPath = element;
}
}
scanner.close();
}
} catch (Exception e) {
Log.e(TAG, "Ошибка при сканировании директории", e);
}
}
}
if (sdCardPath == null) {
sdCardPath = App.getContext().getFilesDir().getAbsolutePath();
}
Log.i(TAG, "sdcard path is: " + sdCardPath);
return sdCardPath;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment