2014-09-30 79 views
0

我試圖得到正確的路徑,SD卡是我的三星S4的Android設備中通過我的應用程序,但是當我嘗試上述路徑:獲取正確的路徑外部SD卡中的Android

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath(); 
     String pathTwo = Environment.getExternalStorageDirectory().getAbsolutePath(); 
     String path3 = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath(); 

它獲取/ storage/emulated/0的私人不可寫路徑,而不是正確的存儲路徑/ sdcard1

我通過使用Android設備監視器的文件資源管理器找到了正確的路徑,但我不想硬編碼路徑因爲路徑可能因設備而異。

親切的問候

回答

3

基於前面的回答,路徑到外置SD卡其實變化與不同的設備製造商。

Environment.getExternalStorageDirectory()」是指任何設備製造商認爲是「外部存儲」的設備,在某些設備上,這是可移動介質,如SD卡,在某些設備上,這是部分設備上的閃存。 ,「外部存儲」是指「安裝在主機上時通過USB Mass Storage模式訪問的東西」,至少對於Android 1.x和2.x. 但問題是關於外部SD。如何獲得像「/ mnt/sdcard/external_sd」(它可能因設備不同而不同)? 如上所述,Android沒有「外部SD」概念,除了外部存儲設備外 如果設備製造商選擇了外部存儲板上閃光燈,並且還有SD卡,則需要聯繫該製造商以確定磨損不管她是否可以使用SD卡(不保證)以及使用它的規則是什麼,例如使用什麼路徑。「

基於this答案。

所以,沒有絕對的方法通過代碼來獲得這條路徑。

1

正如gilonm提到的那樣,外部(可移動)Sd路徑因設備而異,但我寫了一個方法,它遍歷不同製造商使用的所有不同ext路徑,然後找到完全匹配。

如果找不到路徑,它將返回空字符串。如果找到路徑,您仍然需要驗證卡是否已插入。 (通過檢查該路徑上是否存在子文件夾)

注意:我在方法內使用了StreamSupport庫,因此您需要下載jar文件並將其添加到項目的libs文件夾中,就是這樣會工作的!

public static String getExternalSdPath(Context context) { 
    List<String> listOfFoldersToSearch = Arrays.asList("/storage/", "/mnt/", "/removable/", "/data/"); 
    final List<String> listOf2DepthFolders = Arrays.asList("sdcard0", "media_rw", "removable"); 
    final List<String> listOfExtFolders = Arrays.asList("sdcard1", "extsdcard", "external_sd", "microsd", "emmc", "ext_sd", "sdext", 
      "sdext1", "sdext2", "sdext3", "sdext4"); 
    final String[] thePath = {""}; 
    Optional<File> optional = StreamSupport.stream(listOfFoldersToSearch) 
      .filter(new Predicate<String>() { 
       @Override 
       public boolean test(final String s) { 
        File folder = new File(s); 
        return folder.exists() && folder.isDirectory(); 
       } 
      }) //I got the ones that exist and are directories 
      .flatMap(new Function<String, Stream<File>>() { 
       @Override 
       public Stream<File> apply(final String s) { 
        try { 
         List<File> files = Arrays.asList(new File(s).listFiles()); 
         return StreamSupport.stream(files); 
        } catch (NullPointerException e) { 
         return StreamSupport.stream(new ArrayList<File>()); 
        } 
       } 
      }) //I got all sub-dirs of the main folders 
      .flatMap(new Function<File, Stream<File>>() { 
       @Override 
       public Stream<File> apply(final File file1) { 
        if (listOf2DepthFolders.contains(file1.getName() 
          .toLowerCase())) { 
         try { 
          List<File> 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(new Predicate<File>() { 
       @Override 
       public boolean test(final File o) { 
        return listOfExtFolders.contains(o.getName() 
          .toLowerCase()); 
       } 
      }) 
      .findFirst(); 

    optional.ifPresent(new Consumer<File>() { 
     @Override 
     public void accept(final File file) { 
      thePath[0] = file.getAbsolutePath(); 
     } 
    }); 

    Log.e("Path", thePath[0]); 

    try { 
     ContextCompat.getExternalFilesDirs(context, null); 
    } catch (Exception e) { 
     Log.e("PathException", thePath[0]); 
    } 
    return thePath[0]; 
} 

P.S.我在幾臺HTC和三星設備上進行了測試和驗證。

3

對於多設備測試的所有設備

String sSDpath = null; 
      File fileCur = null; 
      for(String sPathCur : Arrays.asList("MicroSD","external_SD","sdcard1","ext_card", "external_sd", "ext_sd", "external", "extSdCard", "externalSdCard")) // external sdcard 
      { 

       fileCur = new File("/mnt/", sPathCur); 
       if(fileCur.isDirectory() && fileCur.canWrite()) 
       { 
        sSDpath = fileCur.getAbsolutePath(); 
        break; 
       } 
       if(sSDpath == null) { 
       fileCur = new File("/storage/", sPathCur); 
       if(fileCur.isDirectory() && fileCur.canWrite()) 
       { 
        sSDpath = fileCur.getAbsolutePath(); 
        break; 
       } 
       } 
       if(sSDpath == null) { 
       fileCur = new File("/storage/emulated", sPathCur); 
       if(fileCur.isDirectory() && fileCur.canWrite()) 
       { 
        sSDpath = fileCur.getAbsolutePath(); 
Log.e("path",sSpath); 
        break; 
       } 
       } 
      } 

100%工作。