简体   繁体   中英

I am not able get root path usb otg in Android Nougat .but able to get root path of sd card

I am not able get root path usb otg in Android Nougat working fine till marshmallow. even able to get root path of sd card .can any body help me out from this i am frustrated from couple days.

Here is my code that return root path upto marshmallow and nougat sdcard. but not 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();

You will not get a listing in Nougat for the root directory. You could have told us that.

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

Nougat does not allow listing root. There are several other directories too that you cannot list anymore under Nougat.

You can check this approach on Nougat. But there is no way to make difference between removable SD card and USB flash if they are both connected to your device simultaneously.

Your approach - parsing mount file - does not work for some (chineese?) devices because string entry for internal memory may be completely the same as for removable SD card.

PS It is a user responsibility to find out where is USB flash or removable SD card in a "well" designed app. You should not do that by himself because Android does not provide public API for this purpose except Intent.ACTION_OPEN_DOCUMENT_TREE to call a built-in file chooser to interact with user in order to choose folder.

PPS INTERACTION WITH USER: Create button with name "Show USB OTG Root" and onClick method containing

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);//http://stackoverflow.com/questions/28605278/android-5-sd-card-label
startActivityForResult(intent, REQUEST_CODE_USB_ACCESS);

In onActivityResult callback you have to catch user answer when he choose USB OTG root in internal Android chooser:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    switch (requestCode) { 
        case REQUEST_CODE_USB_ACCESS:                    
            if (data.getData() != null) {
                int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                getContentResolver().takePersistableUriPermission(data.getData(), takeFlags);
                DocumentFile documentFile = DocumentFile.fromTreeUri(this, data.getData());
            }
    }
}

documentFile is an access object representing USB OTG root (if user did not make mistake when choosing). You can make some file operation like documentFile.listFiles() on it. There is no other way to operate with files on removable media in public API starting from Lollipop. Ie your desired path to USB OTG can not be obtained as a string from some public API method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM