gpt4 book ai didi

android - 如何找到挂载到android的USB路径?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:03:35 25 4
gpt4 key购买 nike

我正在开发一个从 USB 读取数据的 Android 应用程序。USB可以通过串行端口连接到android,我的应用程序可以找到它。

现在,我想从USB读取数据文件和文件夹。我读过很多文章。我发现他们使用这段代码:

Environment.getExternalStorageDirectory();

但是在我的例子中,我得到的路径是/storage/emulated/0。当我尝试读取路径中包含的所有文件时,我得到以下语句:

/storage/emulated/0/Android
/storage/emulated/0/Music
/storage/emulated/0/Podcasts
/storage/emulated/0/Ringtones

等等

但是找不到我的U盘路径。所以,我不确定这是从 USB 读取文件的正确方法吗?

这是我的代码:

File f = Environment.getExternalStorageDirectory();
File[] files = f.listFiles();
String fol = "";
for (File inFile : files) {
if (inFile.isDirectory()) {
fol += inFile.toString()+"\n";
}
}
TextView tv = (TextView) findViewById(R.id.demoTitle);
tv.setText(fol);

最佳答案

使用这段代码获取所有挂载的设备

public String getStoragepath() {
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
String line;
String[] patharray = new String[10];
int i = 0;
int available = 0;

BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
String mount = new String();
if (line.contains("secure"))
continue;
if (line.contains("asec"))
continue;

if (line.contains("fat")) {// TF card
String columns[] = line.split(" ");
if (columns != null && columns.length > 1) {
mount = mount.concat(columns[1] + "/requiredfiles");

patharray[i] = mount;
i++;

// check directory is exist or not
File dir = new File(mount);
if (dir.exists() && dir.isDirectory()) {
// do something here

available = 1;
finalpath = mount;
break;
} else {

}
}
}
}
if (available == 1) {

} else if (available == 0) {
finalpath = patharray[0];
}

} catch (Exception e) {

}
return finalpath;
}

关于android - 如何找到挂载到android的USB路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16262830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com