如何找到它安装到Android USB的路径? [英] How to find the path of USB which is mounted to android?

查看:285
本文介绍了如何找到它安装到Android USB的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android应用程序从USB读取数据。
该USB可以连接带有串口Android和我的应用程序可以找到它。

现在,我想从USB读取数据文件和文件夹。我已经看了很多文章。我发现他们使用code:

  Environment.getExternalStorageDirectory();

但在我的情况,我得到的路径是/存储/模拟/ 0。
当我试着去阅读所有这些都包含在路径中的文件,我得到了下面的语句:

  /存储/模拟/ 0 / Android版
/存储/模拟/ 0 /音乐
/存储/模拟/ 0 /播客
/存储/模拟/ 0 /铃声

等。

但我的USB的路径没有找到。所以,我不知道它是从USB读取文件的正确方法?

下面是我的code:

 文件f = Environment.getExternalStorageDirectory();
文件[] =文件f.listFiles();
字符串FOL =;
对于(文件INFILE:文件){
    如果(inFile.isDirectory()){
        FOL + = inFile.toString()+\\ n;
    }
}
TextView的电视=(的TextView)findViewById(R.id.demoTitle);
tv.setText(FOL);


解决方案

使用此code获得所有已安装的设备

 公共字符串getStoragepath(){
    尝试{
        运行运行时间=调用Runtime.getRuntime();
        流程PROC =的Runtime.exec(摩的);
        InputStream为= proc.getInputStream();
        InputStreamReader的ISR =新的InputStreamReader(是);
        串线;
        的String [] = patharray新的String [10];
        INT I = 0;
        INT可用= 0;        BR的BufferedReader =新的BufferedReader(ISR);
        而((行= br.readLine())!= NULL){
            串装=新的String();
            如果(line.contains(安全))
                继续;
            如果(line.contains(ASEC))
                继续;            如果(line.contains(胖)){// TF卡
                字符串列[] = line.split();
                如果(列=空&放大器;!&放大器; columns.length→1){
                    坐骑= mount.concat(列[1] +/ requiredfiles);                    patharray [I] =安装;
                    我++;                    //检查目录存在或不
                    文件DIR =新的文件(安装);
                    如果(dir.exists()及&放大器; dir.isDirectory()){
                        //这里做什么                        可用= 1;
                        finalpath =安装;
                        打破;
                    }其他{                    }
                }
            }
        }
        如果(购== 1){        }如果别人(可用== 0){
            finalpath = patharray [0];
        }    }赶上(例外五){    }
    返回finalpath;
}

I am working on an android application to read data from USB. The usb can be connected to android with serial port and my app can find it.

Now, I want to read data files and folder from USB. I have read many articles. I found that they use this code :

Environment.getExternalStorageDirectory();

However in my case, I got that the path is /storage/emulated/0. When I try to read all the files which are contained in the path, I got the following statements:

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

and etc.

but the path of my usb is not found. So, I'm not sure is it the correct way to read files from USB?

Here is my code :

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

解决方案

get all mounted device by using this code

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 USB的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆