Removable storage (external) sdcard path by manufacturers

后端 未结 4 962
别跟我提以往
别跟我提以往 2020-12-04 18:15

I\'ve been Googling around but it is sooooo hard to find what manufacturers/models use which path for sdcard/external storage.

  • I am NOT talking about the inter
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 18:36

    Based on the paths oceanuz provided, I wrote a method, which locates the external storage path (ignoring the case) and returns it as a String.

    Note: I used StreamSupport library inside my method, so in order for it to work, you'll need to download the jar file and add that to libs folder in your project and that's it.

      public static String getExternalSdPath(Context context) {
        List listOfFoldersToSearch = Arrays.asList("/storage/", "/mnt/", "/removable/", "/data/");
        List listOf2DepthFolders = Arrays.asList("sdcard0", "media_rw", "removable");
        List listOfExtFolders = Arrays.asList("sdcard1", "extsdcard", "external_sd", "microsd", "emmc", "ext_sd", "sdext",
                "sdext1", "sdext2", "sdext3", "sdext4");
        final String[] thePath = {""};
        Optional optional = StreamSupport.stream(listOfFoldersToSearch)
                .filter(s -> {
                    File folder = new File(s);
                    return folder.exists() && folder.isDirectory();
                }) //I got the ones that exist and are directories
                .flatMap(s -> {
                    try {
                        List files = Arrays.asList(new File(s).listFiles());
                        return StreamSupport.stream(files);
                    } catch (NullPointerException e) {
                        return StreamSupport.stream(Collections.emptyList());
                    }
                }) //I got all sub-dirs of the main folders
                .flatMap(file1 -> {
                    if (listOf2DepthFolders.contains(file1.getName().toLowerCase())) {
                        try {
                            List files = Arrays.asList(file1.listFiles());
                            return StreamSupport.stream(files);
                        } catch (NullPointerException e) {
                            return StreamSupport.stream(Collections.singletonList(file1));
                        }
                    } else
                        return StreamSupport.stream(Collections.singletonList(file1));
                }) //Here I got all the 2 depth and 3 depth folders
                .filter(o -> listOfExtFolders.contains(o.getName().toLowerCase()))
                .findFirst();
    
        optional.ifPresent(file -> thePath[0] = file.getAbsolutePath());
    
        Log.i("Path", thePath[0]);
    
        return thePath[0];
    }
    

    This method will return an empty string if there is no Sd Card path found.

提交回复
热议问题