簡體   English   中英

我無法在Android Nougat中獲得根路徑usb otg。但是能夠獲得SD卡的根路徑

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

我無法在Android Nougat中獲得根路徑usb otg正常工作直到棉花糖。 甚至能夠獲得SD卡的根路徑。可以任何身體幫我解決這個問題我幾天都很沮喪。

這是我的代碼返回根路徑到marshmallow和nougat sdcard。 但不是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不允許列出root。 還有其他幾個目錄,你不能再在Nougat下列出了。

您可以在Nougat上查看此方法 但如果可移動SD卡和USB閃存同時連接到您的設備,則無法區分它們。

你的方法-解析掛載文件-不會為某些(?,葡萄牙語)設備工作,因為內部存儲器的字符串項可能是完全一樣的可移動SD卡。

PS用戶有責任在“設計良好”的應用程序中查找USB閃存或可移動SD卡的位置。 你不應該自己這樣做,因為Android沒有為此目的提供公共API ,除了Intent.ACTION_OPEN_DOCUMENT_TREE調用內置文件選擇器與用戶交互以選擇文件夾。

PPS與USER交互:創建名為“Show USB OTG Root”的按鈕和包含的onClick方法

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

onActivityResult回調中,當他在內部Android選擇器中選擇USB OTG root時,您必須捕獲用戶答案:

@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是一個表示USB OTG root的訪問對象(如果用戶在選擇時沒有出錯)。 您可以在其上進行一些文件操作,如documentFile.listFiles()。 從Lollipop開始,沒有其他方法可以在公共API中的可移動媒體上使用文件。 即你想要的USB OTG路徑不能從一些公共API方法獲得。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM