1

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;
    }
Asif khan
  • 31
  • 4

2 Answers2

0
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.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • nopes we can to listing in nougat . but we have give runtime permission – Asif khan May 25 '17 at 07:58
  • Again: you should have told us that you did not get a listing for root to begin with. – greenapps May 25 '17 at 08:14
  • i don't think you understand actual scenario. once user accept permission at runtime you listFiles are work fine and you can use. File dir = new File ("/"); File [] files = dir.listFiles(); – Asif khan May 25 '17 at 09:26
  • Yes you can code that but as said the listing will be null or empty for that root folder. Thats Nougat. – greenapps May 25 '17 at 10:33
  • https://stackoverflow.com/questions/40418810/android-7-0-not-able-to-get-list-of-files-under-directory – greenapps May 25 '17 at 10:41
0

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.

P.S. 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.

P.P.S 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. I.e. your desired path to USB OTG can not be obtained as a string from some public API method.

isabsent
  • 3,683
  • 3
  • 25
  • 46
  • Can you please help me out how to get root path otg in nougat ..mounting is work till marshmallow even sdcard card in nougat . – Asif khan May 26 '17 at 09:24
  • I have used this way https://stackoverflow.com/a/34703295/753575 to get USB OTG root by means of reflection (because Android did not provide public API for that) and therefore this way may not find the OTG root path for all Android devices. The only way to get it - interact with user and he gives your the path. Provided that he is able to make difference between internal memory, removable SD card and USB OTG in internal Android file chooser... – isabsent May 26 '17 at 09:37