有没有一种通用的方法来找到外置SD卡的位置?
请不要与外部存储混淆。
Environment.getExternalStorageState()返回内部SD挂载点的路径,例如/mnt/sdcard。但问题是关于外部SD的。我如何获得类似/mnt/sdcard/external_sd的路径(它可能因设备而异)?
我想我将以根据文件系统名称过滤mount命令的输出作为结束。但我不确定这种方式是否足够稳健。
有没有一种通用的方法来找到外置SD卡的位置?
请不要与外部存储混淆。
Environment.getExternalStorageState()返回内部SD挂载点的路径,例如/mnt/sdcard。但问题是关于外部SD的。我如何获得类似/mnt/sdcard/external_sd的路径(它可能因设备而异)?
我想我将以根据文件系统名称过滤mount命令的输出作为结束。但我不确定这种方式是否足够稳健。
当前回答
这是我用来找到外部卡的方法。使用mount cmd返回然后解析vfat部分。
String s = "";
try {
Process process = new ProcessBuilder().command("mount")
.redirectErrorStream(true).start();
process.waitFor();
InputStream is = process.getInputStream();
byte[] buffer = new byte[1024];
while (is.read(buffer) != -1) {
s = s + new String(buffer);
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}
//用行分隔mount列表
String[] lines = s.split("\n");
for(int i=0; i<lines.length; i++) {
//如果行内有挂载路径且为vfat类型,说明可能是内置或者外置sd的挂载点
if(-1 != lines[i].indexOf(path[0]) && -1 != lines[i].indexOf("vfat")) {
//再用空格分隔
String[] blocks = lines[i].split("\\s");
for(int j=0; j<blocks.length; j++) {
//判断是否是挂载为vfat类型
if(-1 != blocks[j].indexOf(path[0])) {
//Test if it is the external sd card.
}
}
}
}
其他回答
它适用于所有外部设备,但确保只获得外部设备文件夹名,然后你需要从给定的位置使用文件类获取文件。
public static List<String> getExternalMounts() {
final List<String> out = new ArrayList<>();
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;
}
调用:
List<String> list=getExternalMounts();
if(list.size()>0)
{
String[] arr=list.get(0).split("/");
int size=0;
if(arr!=null && arr.length>0) {
size= arr.length - 1;
}
File parentDir=new File("/storage/"+arr[size]);
if(parentDir.listFiles()!=null){
File parent[] = parentDir.listFiles();
for (int i = 0; i < parent.length; i++) {
// get file path as parent[i].getAbsolutePath());
}
}
}
访问外部存储
为了读写外部存储上的文件,你的应用程序必须获得READ_EXTERNAL_STORAGE或WRITE_EXTERNAL_STORAGE系统权限。例如:
<manifest ...>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
...
</manifest>
这次我尝试了这个主题内的所有解决方案。但是,在有一个外置卡(可移动)和一个内置卡(不可移动)的设备上,所有这些设备都不能正常工作。外部卡的路径不可能从'mount'命令,从'proc/mounts'文件等。
我在Paulo Luan的基础上提出了自己的解决方案:
String sSDpath = null;
File fileCur = null;
for( String sPathCur : Arrays.asList( "ext_card", "external_sd", "ext_sd", "external", "extSdCard", "externalSdCard")) // external sdcard
{
fileCur = new File( "/mnt/", sPathCur);
if( fileCur.isDirectory() && fileCur.canWrite())
{
sSDpath = fileCur.getAbsolutePath();
break;
}
}
fileCur = null;
if( sSDpath == null) sSDpath = Environment.getExternalStorageDirectory().getAbsolutePath();
通过编写下面的代码,您将获得位置:
数据存储663D-554E / Android / app_package_name / files /
存储你的应用程序数据在/android/数据位置内的sd_card。
File[] list = ContextCompat.getExternalFilesDirs(MainActivity.this, null);
list[1]+"/fol"
对于获取位置,内部传递0,sdcard传递1到文件数组。
我已经在moto g4 plus和三星设备上测试了这段代码(一切正常)。
希望这对你有所帮助。
Environment.getExternalStorageState()返回内部SD挂载点的路径,如"/mnt/sdcard"
不,Environment.getExternalStorageDirectory()指的是设备制造商认为是“外部存储”的任何东西。在某些设备上,这是可移动媒体,如SD卡。在某些设备上,这是设备上flash的一部分。这里的“外部存储”指的是“挂载在主机上通过USB大容量存储模式访问的东西”,至少对于Android 1来说是这样。X和2.x。
但问题是关于外置SD。如何获得像“/mnt/sdcard/external_sd”这样的路径(它可能因设备而异)?
Android没有“外部SD”的概念,除了如上所述的外部存储。
如果设备制造商已经选择将外部存储作为板载闪存,并且还具有SD卡,那么您将需要联系该制造商以确定是否可以使用SD卡(不保证)以及使用它的规则,例如使用它的路径。
更新
最近有两件事值得注意:
首先,在Android 4.4+上,你没有对可移动媒体(例如,“外部SD”)的写访问权,除非该媒体上的任何位置可能由getExternalFilesDirs()和getExternalCacheDirs()返回。请参阅Dave Smith对此的出色分析,特别是如果您想了解底层细节的话。
其次,为了避免有人质疑可移动媒体访问是否是Android SDK的一部分,以下是Dianne Hackborn的评估:
...keep in mind: until Android 4.4, the official Android platform has not supported SD cards at all except for two special cases: the old school storage layout where external storage is an SD card (which is still supported by the platform today), and a small feature added to Android 3.0 where it would scan additional SD cards and add them to the media provider and give apps read-only access to their files (which is also still supported in the platform today). Android 4.4 is the first release of the platform that has actually allowed applications to use SD cards for storage. Any access to them prior to that was through private, unsupported APIs. We now have a quite rich API in the platform that allows applications to make use of SD cards in a supported way, in better ways than they have been able to before: they can make free use of their app-specific storage area without requiring any permissions in the app, and can access any other files on the SD card as long as they go through the file picker, again without needing any special permissions.
通过读取/proc/mounts(标准的Linux文件),并对vold数据(/system/etc/vold.conf)进行交叉检查,可以找到任何其他SD卡被挂载的位置。注意,Environment.getExternalStorageDirectory()返回的位置可能不会出现在vold配置中(在某些设备中,它是不能卸载的内部存储),但仍然必须包含在列表中。然而,我们并没有找到一个向用户描述它们的好方法。