Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the list of mounted external storage of android device

Tags:

Here we are developing android app for android TV box. How can I get the list of mounted external storage like USB stick, SDCARD and SATA HDD.

like image 772
Android_Qmax Avatar asked Feb 18 '12 10:02

Android_Qmax


People also ask

What is external storage path in Android?

Android External Storage Example Code getExternalFilesDir(): It returns the path to files folder inside Android/data/data/application_package/ on the SD card. It is used to store any required files for your app (like images downloaded from web or cache files).

What is internal and external storage in Android?

In short, Internal Storage is for apps to save sensitive data to which other apps and users cannot access. However, Primary External Storage is part of built-in storage which can be accessed (for read-write) by the user and other apps but with permissions.


2 Answers

I use /proc/mounts file to get the list of available storage options

public class StorageUtils {      private static final String TAG = "StorageUtils";      public static class StorageInfo {          public final String path;         public final boolean readonly;         public final boolean removable;              public final int number;          StorageInfo(String path, boolean readonly, boolean removable, int number) {             this.path = path;             this.readonly = readonly;             this.removable = removable;                      this.number = number;         }          public String getDisplayName() {             StringBuilder res = new StringBuilder();             if (!removable) {                 res.append("Internal SD card");             } else if (number > 1) {                 res.append("SD card " + number);             } else {                 res.append("SD card");             }             if (readonly) {                 res.append(" (Read only)");             }             return res.toString();         }     }      public static List<StorageInfo> getStorageList() {          List<StorageInfo> list = new ArrayList<StorageInfo>();         String def_path = Environment.getExternalStorageDirectory().getPath();         boolean def_path_removable = Environment.isExternalStorageRemovable();         String def_path_state = Environment.getExternalStorageState();         boolean def_path_available = def_path_state.equals(Environment.MEDIA_MOUNTED)                                     || def_path_state.equals(Environment.MEDIA_MOUNTED_READ_ONLY);         boolean def_path_readonly = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY);          HashSet<String> paths = new HashSet<String>();         int cur_removable_number = 1;          if (def_path_available) {             paths.add(def_path);             list.add(0, new StorageInfo(def_path, def_path_readonly, def_path_removable, def_path_removable ? cur_removable_number++ : -1));         }          BufferedReader buf_reader = null;         try {             buf_reader = new BufferedReader(new FileReader("/proc/mounts"));             String line;             Log.d(TAG, "/proc/mounts");             while ((line = buf_reader.readLine()) != null) {                 Log.d(TAG, line);                 if (line.contains("vfat") || line.contains("/mnt")) {                     StringTokenizer tokens = new StringTokenizer(line, " ");                     String unused = tokens.nextToken(); //device                     String mount_point = tokens.nextToken(); //mount point                     if (paths.contains(mount_point)) {                         continue;                     }                     unused = tokens.nextToken(); //file system                     List<String> flags = Arrays.asList(tokens.nextToken().split(",")); //flags                     boolean readonly = flags.contains("ro");                      if (line.contains("/dev/block/vold")) {                         if (!line.contains("/mnt/secure")                             && !line.contains("/mnt/asec")                             && !line.contains("/mnt/obb")                             && !line.contains("/dev/mapper")                             && !line.contains("tmpfs")) {                             paths.add(mount_point);                             list.add(new StorageInfo(mount_point, readonly, true, cur_removable_number++));                         }                     }                 }             }          } catch (FileNotFoundException ex) {             ex.printStackTrace();         } catch (IOException ex) {             ex.printStackTrace();         } finally {             if (buf_reader != null) {                 try {                     buf_reader.close();                 } catch (IOException ex) {}             }         }         return list;     } } 
like image 50
Vitaliy Polchuk Avatar answered Nov 17 '22 03:11

Vitaliy Polchuk


To get all available external storage folders (including SD cards), you can use this :

File[] externalStorageFiles=ContextCompat.getExternalFilesDirs(this,null); 

To go to the "root" of each external storage (the one that is the real mounting folder path), you can use this function I've made:

  /** Given any file/folder inside an sd card, this will return the path of the sd card */   private static String getRootOfInnerSdCardFolder(File file)     {     if(file==null)       return null;     final long totalSpace=file.getTotalSpace();     while(true)       {       final File parentFile=file.getParentFile();       if(parentFile==null||parentFile.getTotalSpace()!=totalSpace)         return file.getAbsolutePath();       file=parentFile;       }     } 
like image 23
android developer Avatar answered Nov 17 '22 04:11

android developer