How to get SD_Card path in android6.0 programmatically

后端 未结 4 1530
既然无缘
既然无缘 2020-12-02 15:12

I am trying to check whether device having external storage or not by using external storage path like this given below

 if (new File(\"/ext_card/\").exists(         


        
4条回答
  •  误落风尘
    2020-12-02 15:25

    Here's my solution, which is guaranteed to work till Android 7.0 Nougat:

    /* returns external storage paths (directory of external memory card) as array of Strings */
    public String[] getExternalStorageDirectories() {
    
            List results = new ArrayList<>();
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //Method 1 for KitKat & above
            File[] externalDirs = getExternalFilesDirs(null);
            String internalRoot = Environment.getExternalStorageDirectory().getAbsolutePath().toLowerCase();
    
            for (File file : externalDirs) {
                if(file==null) //solved NPE on some Lollipop devices
                   continue;
                String path = file.getPath().split("/Android")[0];
    
                if(path.toLowerCase().startsWith(internalRoot))
                   continue;
    
                boolean addPath = false;
    
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    addPath = Environment.isExternalStorageRemovable(file);
                }
                else{
                    addPath = Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(file));
                }
    
                if(addPath){
                    results.add(path);
                }
            }
        }
    
            if(results.isEmpty()) { //Method 2 for all versions
                // better variation of: http://stackoverflow.com/a/40123073/5002496
                String output = "";
                try {
                    final Process process = new ProcessBuilder().command("mount | grep /dev/block/vold")
                    .redirectErrorStream(true).start();
                    process.waitFor();
                    final InputStream is = process.getInputStream();
                    final byte[] buffer = new byte[1024];
                    while (is.read(buffer) != -1) {
                        output = output + new String(buffer);
                    }
                    is.close();
                } catch (final Exception e) {
                    e.printStackTrace();
                }
                if(!output.trim().isEmpty()) {
                    String devicePoints[] = output.split("\n");
                    for(String voldPoint: devicePoints) {
                        results.add(voldPoint.split(" ")[2]);
                    }
                }
            }
    
            //Below few lines is to remove paths which may not be external memory card, like OTG (feel free to comment them out)
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                for (int i = 0; i < results.size(); i++) {
                    if (!results.get(i).toLowerCase().matches(".*[0-9a-f]{4}[-][0-9a-f]{4}")) {
                        Log.d(LOG_TAG, results.get(i) + " might not be extSDcard");
                        results.remove(i--);
                    }
                }
            } else {
                for (int i = 0; i < results.size(); i++) {
                    if (!results.get(i).toLowerCase().contains("ext") && !results.get(i).toLowerCase().contains("sdcard")) {
                        Log.d(LOG_TAG, results.get(i)+" might not be extSDcard");
                        results.remove(i--);
                    }
                }
            }
    
            String[] storageDirectories = new String[results.size()];
            for(int i=0; i

提交回复
热议问题