android 外部存储列表,如何获取Android设备的已安装外部存储列表

我使用/ 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 ListgetStorageList() { Listlist = new ArrayList(); 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); HashSetpaths = new HashSet(); 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 Listflags = 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);

要转到每个外部存储的“根”(即真正的安装文件夹路径),您可以使用我所做的这个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挂载点的路径,如“/ mnt / sdcard”

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

但问题是关于外部SD。 如何获得像“/ mnt / sdcard / external_sd”这样的路径(它可能因设备而异)?

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

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

试试这个:

的Manifest.xml:

Myreceiver:

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

另一种选择。

首先获取所有外部文件目录

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

我现在这个话题很老但这可能有所帮助。 你应该使用这个方法。

System.getenv();

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值