java - 我无法在 Android Nougat 中获取 USB otg 的根路径。但能够获取 sd 卡的根路径

标签 java android

我无法在 Android Nougat 中获取根路径 USB otg,直到棉花糖为止正常工作。甚至能够获得SD卡的根路径。任何人都可以帮助我摆脱这个困境,这几天我很沮丧。

这是我的代码,返回棉花糖和牛轧糖SD卡的根路径。但不是 USB otg

public static String FileSystem() {
        String path = null;
        String SD_CARD_DIR = null;
        try {
            Process mount = Runtime.getRuntime().exec("mount");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(mount.getInputStream()));
            mount.waitFor();

//            String extPath = Environment.getExternalStorageDirectory().getPath();
//            String isMedai = Environment.getExternalStorageState();
//            if(Environment.MEDIA_MOUNTED.equalsIgnoreCase(isMedai)){
//                String root = Environment.getRootDirectory().getPath();
//                path  = Environment.getExternalStoragePublicDirectory(DIRECTORY_EDUCOMP).getPath();
//            }
            String line;
            String strFileSystem = null;
            while ((line = bufferedReader.readLine()) != null) {
                String[] split = line.split("\\s+");
                for (int i = 0; i < split.length - 1; i++) {
                    if (SD_CARD_DIR == null) {
                        File mainroot = new File(split[i]);
                        File f[] = mainroot.listFiles(new FilenameFilter() {
                            @Override
                            public boolean accept(File dir, String name) {
                                return new File(dir, name).isDirectory();
                            }
                        }); // Get First level folders /mnt
                        if (f != null) {
                            for (File aFile : f) {
                                File[] filenames = aFile.listFiles(); // Get second level
                                // folders
                                // /mnt/sdcard so on
                                // and math Educomp
                                // folder
                                if (filenames != null) {
                                    for (File ff : filenames) {
                                        String eduFileName = ff.getName();
                                        if (eduFileName.equals("Temp")) {
                                            File[] listEducompfile = ff.listFiles();
                                            if (listEducompfile != null) {
                                                for (File fff : listEducompfile) {
                                                    String contentFileName = fff.getName();
                                                    if (contentFileName.equals("ts")) {
                                                        SD_CARD_DIR = aFile
                                                                .getAbsolutePath() + "/";
                                                        break;
                                                    }
                                                }

                                            }
                                        } else {
                                            File[] filenamesList = ff.listFiles(new FilenameFilter() {
                                                @Override
                                                public boolean accept(File dir, String name) {
                                                    return new File(dir, name).isDirectory();
                                                }
                                            });
                                            if (filenamesList != null) {
                                                for (File fff : filenamesList) {
                                                    String eduFileNamess = fff.getName();
                                                    if (eduFileNamess.equals("Temp")) {
                                                        File[] listEducompfile = fff.listFiles();
                                                        if (listEducompfile != null) {
                                                            for (File fffds : listEducompfile) {
                                                                String contentFileName = fffds.getName();
                                                                if (contentFileName.equals("ts")) {
                                                                    return SD_CARD_DIR = ff + "/";

                                                                }
                                                            }

                                                        }
                                                    }
                                                }
                                            }

                                        }
                                    }
                                }

                            }
                        }
                    }

                    // SD_CARD_DIR = DEFAULT_SD_CARD_DIR;
                }

                return SD_CARD_DIR;
            }

            return path;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

最佳答案

File dir = new File ("/");

File files = dir.listFiles();

您不会在 Nougat 中获得根目录的列表。你本可以告诉我们这一点。

`files==null` or files.length()==0

Nougat 不允许列出根目录。还有其他几个目录,您无法在 Nougat 下列出。

关于java - 我无法在 Android Nougat 中获取 USB otg 的根路径。但能够获取 sd 卡的根路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44174460/

相关文章:

java - liferay 上的 Grails portlet

android - TCP 和 UDP 无线与谷歌眼镜

Android无需人工交互即可连接WiFi

java - 如何获取方法名称?

java - 如何区分一个pom.xml中的不同wsdl位置-Maven

java - 如何在 Android 中渲染 PDF 文件

android - 如何在修改后更新 Android View ?

android - 从 Android 调用 REST (POST) 网络服务

java - 我可以在具有 JRE 1.6 的系统上运行使用 Java 1.8 构建的应用程序吗?

具有 VIM 集成的 Java IDE,适用于来自 C/C++ 的人