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

前端 未结 26 2738
清歌不尽
清歌不尽 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条回答
  •  旧时难觅i
    2020-11-22 05:40

    Here's how I get the list of SD-card paths (excluding the primary external storage) :

      /**
       * returns a list of all available sd cards paths, or null if not found.
       * 
       * @param includePrimaryExternalStorage set to true if you wish to also include the path of the primary external storage
       */
      @TargetApi(Build.VERSION_CODES.HONEYCOMB)
      public static List getSdCardPaths(final Context context,final boolean includePrimaryExternalStorage)
        {
        final File[] externalCacheDirs=ContextCompat.getExternalCacheDirs(context);
        if(externalCacheDirs==null||externalCacheDirs.length==0)
          return null;
        if(externalCacheDirs.length==1)
          {
          if(externalCacheDirs[0]==null)
            return null;
          final String storageState=EnvironmentCompat.getStorageState(externalCacheDirs[0]);
          if(!Environment.MEDIA_MOUNTED.equals(storageState))
            return null;
          if(!includePrimaryExternalStorage&&VERSION.SDK_INT>=VERSION_CODES.HONEYCOMB&&Environment.isExternalStorageEmulated())
            return null;
          }
        final List result=new ArrayList<>();
        if(includePrimaryExternalStorage||externalCacheDirs.length==1)
          result.add(getRootOfInnerSdCardFolder(externalCacheDirs[0]));
        for(int i=1;i

提交回复
热议问题