我怎样才能得到安装Android设备的外部存储列表

在这里,我们正在开发android电视盒的android应用程序。 我怎样才能得到像USB stick, SDCARD and SATA HDD.挂载的外部存储列表USB stick, SDCARD and SATA HDD.

我使用/ proc / mounts文件来获取可用存储选项的列表

 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; } } 

要获得所有可用的外部存储文件夹(包括SD卡),可以使用以下命令:

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

要转到每个外部存储(即真正的挂载文件夹path)的“根目录”,可以使用我所做的这个function:

  /** 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; } } 

Environment.getExternalStorageState()返回内部SD挂载点的path,如“/ mnt / sdcard”

不, Environment.getExternalStorageDirectory()是指任何设备制造商认为是“外部存储”。 在某些设备上,这是可移动媒体,如SD卡。 在某些设备上,这是设备上闪存的一部分。 在这里,“外部存储”是指“安装在主机上时通过USB大容量存储模式访问的东西”,至less对于Android 1.x和2.x.

但问题是关于外部可持续发展。 如何得到像“/ mnt / sdcard / external_sd”这样的path(它可能因设备而异)?

除了外部存储,Android没有“外部SD”的概念,如上所述。

如果设备制造商select将外部存储设备设置为板载闪存并且还具有SD卡,则需要联系制造商以确定您是否可以使用SD卡(不保证)以及规则是什么使用它,比如使用什么path。

试试这个:

的Manifest.xml:

 <receiver android:name=".MyReceiver"> <intent-filter> <action android:name="android.intent.action.ums_connected" /> </intent-filter> </receiver> 

Myreceiver:

 public class MyReceiver extends BroadcastReceiver{ if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_CONNECTED")) {...} } 

另一种select。

首先获取所有外部文件dirs

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

然后为每个文件夹调用这个函数

 private static String getRootOfExternalStorage(File file) { if (file == null) return null; String path = file.getAbsolutePath(); return path.replaceAll("/Android/data/" + getPackageName() + "/files", ""); } 

您可以使用getExternalStorageDirectory()来获取外部存储目录。 该文档给出了一个很好的解释它的用法http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29

对于USB设备,您可能需要查看UsbManager类或更通用的android.hardware.usb http://developer.android.com/reference/android/hardware/usb/UsbManager.html

我现在这个话题是旧的,但这可能有帮助。 你应该使用thi方法。

System.getenv();

请参阅项目Environment3以访问连接到设备的所有存储。

https://github.com/omidfaraji/Environment3