How can I get the external SD card path for Android 4.0+?

前端 未结 26 2720
清歌不尽
清歌不尽 2020-11-22 04:48

Samsung Galaxy S3 has an external SD card slot, which is mounted to /mnt/extSdCard.

How can I get this path by something like Environment.getExter

相关标签:
26条回答
  • 2020-11-22 05:27

    I was using Dmitriy Lozenko's solution until i checked on an Asus Zenfone2, Marshmallow 6.0.1 and the solution is not working. The solution failed when getting EMULATED_STORAGE_TARGET, specifically for microSD path, i.e: /storage/F99C-10F4/. I edited the code to get the emulated root paths directly from emulated application paths with context.getExternalFilesDirs(null); and add more known phone-model-specific physical paths.

    To make our life easier, I made a library here. You can use it via gradle, maven, sbt, and leiningen build system.

    If you like the old-fashioned way, you can also copy paste the file directly from here, but you will not know if there is an update in the future without checking it manually.

    If you have any question or suggestion, please let me know

    0 讨论(0)
  • 2020-11-22 05:28

    Actually in some devices the external sdcard default name is showing as extSdCard and for other it is sdcard1.

    This code snippet helps to find out that exact path and helps to retrieve you the path of external device.

    String sdpath,sd1path,usbdiskpath,sd0path;    
            if(new File("/storage/extSdCard/").exists())
                {
                   sdpath="/storage/extSdCard/";
                   Log.i("Sd Cardext Path",sdpath);
                }
            if(new File("/storage/sdcard1/").exists())
             {
                  sd1path="/storage/sdcard1/";
                  Log.i("Sd Card1 Path",sd1path);
             }
            if(new File("/storage/usbcard1/").exists())
             {
                  usbdiskpath="/storage/usbcard1/";
                  Log.i("USB Path",usbdiskpath);
             }
            if(new File("/storage/sdcard0/").exists())
             {
                  sd0path="/storage/sdcard0/";
                  Log.i("Sd Card0 Path",sd0path);
             }
    
    0 讨论(0)
  • 2020-11-22 05:28

    To access files in my SD card, on my HTC One X (Android), I use this path:

    file:///storage/sdcard0/folder/filename.jpg
    

    Note the tripple "/" !

    0 讨论(0)
  • 2020-11-22 05:33

    I did the following to get acces to all the external sd cards.

    With:

    File primaryExtSd=Environment.getExternalStorageDirectory();
    

    you get the path to the primary external SD Then with:

    File parentDir=new File(primaryExtSd.getParent());
    

    you get the parent dir of the primary external storage, and it is also the parent of all the external sd. Now, you can list all the storage and select the one that you want.

    Hope it is usefull.

    0 讨论(0)
  • 2020-11-22 05:33

    This solution (assembled from other answers to this question) handles the fact (as mentioned by @ono) that System.getenv("SECONDARY_STORAGE") is of no use with Marshmallow.

    Tested and working on:

    • Samsung Galaxy Tab 2 (Android 4.1.1 - Stock)
    • Samsung Galaxy Note 8.0 (Android 4.2.2 - Stock)
    • Samsung Galaxy S4 (Android 4.4 - Stock)
    • Samsung Galaxy S4 (Android 5.1.1 - Cyanogenmod)
    • Samsung Galaxy Tab A (Android 6.0.1 - Stock)

      /**
       * Returns all available external SD-Card roots in the system.
       *
       * @return paths to all available external SD-Card roots in the system.
       */
      public static String[] getStorageDirectories() {
          String [] storageDirectories;
          String rawSecondaryStoragesStr = System.getenv("SECONDARY_STORAGE");
      
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
              List<String> results = new ArrayList<String>();
              File[] externalDirs = applicationContext.getExternalFilesDirs(null);
              for (File file : externalDirs) {
                  String path = file.getPath().split("/Android")[0];
                  if((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Environment.isExternalStorageRemovable(file))
                          || rawSecondaryStoragesStr != null && rawSecondaryStoragesStr.contains(path)){
                      results.add(path);
                  }
              }
              storageDirectories = results.toArray(new String[0]);
          }else{
              final Set<String> rv = new HashSet<String>();
      
              if (!TextUtils.isEmpty(rawSecondaryStoragesStr)) {
                  final String[] rawSecondaryStorages = rawSecondaryStoragesStr.split(File.pathSeparator);
                  Collections.addAll(rv, rawSecondaryStorages);
              }
              storageDirectories = rv.toArray(new String[rv.size()]);
          }
          return storageDirectories;
      }
      
    0 讨论(0)
  • 2020-11-22 05:35

    I guess to use the external sdcard you need to use this:

    new File("/mnt/external_sd/")
    

    OR

    new File("/mnt/extSdCard/")
    

    in your case...

    in replace of Environment.getExternalStorageDirectory()

    Works for me. You should check whats in the directory mnt first and work from there..


    You should use some type of selection method to choose which sdcard to use:

    File storageDir = new File("/mnt/");
    if(storageDir.isDirectory()){
        String[] dirList = storageDir.list();
        //TODO some type of selecton method?
    }
    
    0 讨论(0)
提交回复
热议问题