查找可移动SD卡的位置


204

有通用的方法来查找外部SD卡的位置吗?

请不要与外部存储混淆。

Environment.getExternalStorageState()返回到内部SD安装点的路径,例如“ / mnt / sdcard”。但是问题是关于外部SD。如何获得类似“ / mnt / sdcard / external_sd”的路径(可能因设备而异)?

我想我将以mount文件系统名称过滤命令的输出而结束。但是我不确定这种方式是否足够健壮。


这是直到牛轧糖都可以使用的我的解决方案:stackoverflow.com/a/40205116/5002496
Gokul NC

“ Environment.getExternalStorageState()返回到内部SD安装点的路径,例如“ / mnt / sdcard”。嗯,这并不是Android使用术语“内部”的意思。我相信您要查找的术语是“不可删除的”。
LarsH

Answers:


162

Environment.getExternalStorageState() 返回内部SD安装点的路径,例如“ / mnt / sdcard”

不,Environment.getExternalStorageDirectory()是指设备制造商认为是“外部存储”的任何内容。在某些设备上,这是可移动媒体,例如SD卡。在某些设备上,这是设备上闪存的一部分。在此,“外部存储”是指“至少在Android 1.x和2.x上可以通过安装在主机上的USB Mass Storage模式访问的内容”。

但是问题是关于外部SD。如何获得“ / mnt / sdcard / external_sd”之类的路径(可能因设备而异)?

如上所述,除了外部存储,Android没有“外部SD”的概念。

如果设备制造商选择外部存储为板载闪存,并且还具有SD卡,则需要与该制造商联系,以确定是否可以使用SD卡(不保证)以及适用的规则。使用它,例如使用什么路径。


更新

最近需要注意的两件事:

首先,在Android 4.4+上,您对可移动媒体(例如“外部SD”)没有写权限,除了该媒体上可能由getExternalFilesDirs()和返回的任何位置getExternalCacheDirs()。看到戴夫·史密斯的出色分析对此,尤其是在您需要底层细节时。

其次,以免有人质疑可移动媒体访问是否是Android SDK的一部分,这是Dianne Hackborn的评估

...记住:在Android 4.4之前,官方的Android平台在以下时间不支持SD卡 所有除两种特殊情况:老同学存储布局,其中外部存储SD卡(这仍然是由平台支持的今天) ,并在Android 3.0中添加了一个小功能,它可以扫描其他SD卡并将其添加到媒体提供商,并为应用提供对其文件的只读访问权限(今天该平台仍支持该功能)。

Android 4.4是该平台的第一个版本,实际上已允许应用程序使用SD卡进行存储。在此之前,对它们的任何访问都是通过不支持的私有API进行的。我们现在在平台中拥有一个非常丰富的API,该API允许应用程序以受支持的方式以比以前更好的方式使用SD卡:它们可以免费使用其特定于应用程序的存储区域,而无需任何操作应用程序中的其他权限,并且只要它们通过文件选择器就可以访问SD卡上的任何其他文件,而且无需任何特殊权限。


4
随着HC和ICS设备的问世,“ ExternalStorageDirectory”以及所有其他类似内容都出现在内部物理存储中,这个问题变得越来越重要。最重要的是,大多数用户绝对不知道如何定位其sdcard在文件系统中的位置。
Tony Maro 2012年

283
因此,您的答案基本上是“联系制造商”。没有用。
Dragonroot'3

6
答案的最后一部分不太准确-确实可以通过遵循以下答案(扫描/proc/mounts、/system/etc/vold.fstab等)来检测SD卡路径。
学习OpenGL ES

8
@CommonsWare:尽管如此,当存在可以在许多设备上运行的解决方案时,仍然无法“准确地”联系制造商,此外,SDK本身并不是在所有设备上都保持一致,因此不能保证。即使这些解决方案不能在所有设备上都可以使用,但它们也可以在足够的设备上运行,因此市场上许多Android应用程序都依赖这些技术来检测外部SD卡路径。我认为将所有这些开发人员称为傻子有点苛刻和为时过早-客户肯定不是最终的判断者吗?
了解OpenGL ES

5
@CommonsWare随着事情的发展,这很公平。我绝对同意您的观点,即开发人员不能假设它总是在任何地方都可以使用,并且不能保证任何此类代码都可以在所有设备或所有版本的Android上使用。希望它确实可以在SDK中修复!同时,仍有许多选项可以在许多设备上运行,并且可以改善最终用户的体验,并且在80%成功和0%成功之间进行选择,我将选择80%。
学习OpenGL ES

64

我根据此处找到的一些答案提出了以下解决方案。

码:

public class ExternalStorage {

    public static final String SD_CARD = "sdCard";
    public static final String EXTERNAL_SD_CARD = "externalSdCard";

    /**
     * @return True if the external storage is available. False otherwise.
     */
    public static boolean isAvailable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
        }
        return false;
    }

    public static String getSdCardPath() {
        return Environment.getExternalStorageDirectory().getPath() + "/";
    }

    /**
     * @return True if the external storage is writable. False otherwise.
     */
    public static boolean isWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;

    }

    /**
     * @return A map of all storage locations available
     */
    public static Map<String, File> getAllStorageLocations() {
        Map<String, File> map = new HashMap<String, File>(10);

        List<String> mMounts = new ArrayList<String>(10);
        List<String> mVold = new ArrayList<String>(10);
        mMounts.add("/mnt/sdcard");
        mVold.add("/mnt/sdcard");

        try {
            File mountFile = new File("/proc/mounts");
            if(mountFile.exists()){
                Scanner scanner = new Scanner(mountFile);
                while (scanner.hasNext()) {
                    String line = scanner.nextLine();
                    if (line.startsWith("/dev/block/vold/")) {
                        String[] lineElements = line.split(" ");
                        String element = lineElements[1];

                        // don't add the default mount path
                        // it's already in the list.
                        if (!element.equals("/mnt/sdcard"))
                            mMounts.add(element);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            File voldFile = new File("/system/etc/vold.fstab");
            if(voldFile.exists()){
                Scanner scanner = new Scanner(voldFile);
                while (scanner.hasNext()) {
                    String line = scanner.nextLine();
                    if (line.startsWith("dev_mount")) {
                        String[] lineElements = line.split(" ");
                        String element = lineElements[2];

                        if (element.contains(":"))
                            element = element.substring(0, element.indexOf(":"));
                        if (!element.equals("/mnt/sdcard"))
                            mVold.add(element);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        for (int i = 0; i < mMounts.size(); i++) {
            String mount = mMounts.get(i);
            if (!mVold.contains(mount))
                mMounts.remove(i--);
        }
        mVold.clear();

        List<String> mountHash = new ArrayList<String>(10);

        for(String mount : mMounts){
            File root = new File(mount);
            if (root.exists() && root.isDirectory() && root.canWrite()) {
                File[] list = root.listFiles();
                String hash = "[";
                if(list!=null){
                    for(File f : list){
                        hash += f.getName().hashCode()+":"+f.length()+", ";
                    }
                }
                hash += "]";
                if(!mountHash.contains(hash)){
                    String key = SD_CARD + "_" + map.size();
                    if (map.size() == 0) {
                        key = SD_CARD;
                    } else if (map.size() == 1) {
                        key = EXTERNAL_SD_CARD;
                    }
                    mountHash.add(hash);
                    map.put(key, root);
                }
            }
        }

        mMounts.clear();

        if(map.isEmpty()){
                 map.put(SD_CARD, Environment.getExternalStorageDirectory());
        }
        return map;
    }
}

用法:

Map<String, File> externalLocations = ExternalStorage.getAllStorageLocations();
File sdCard = externalLocations.get(ExternalStorage.SD_CARD);
File externalSdCard = externalLocations.get(ExternalStorage.EXTERNAL_SD_CARD);

1
测试与连结4,连结s,银河s2,银河s3,htc欲望=)
理查德·

2
嗨,理查德(Richard)-信不信由你,我不得不问:您实际上是在尝试写出并以这种方式读入文件中,而不仅仅是得到了麻烦吗?还记得我们以前的“ / sdcard0”问题吗?我尝试了这段代码,但是当我尝试读回它确实写入的文件时,它在S3上失败了。……这很奇怪……又很痛苦:))
霍华德·帕兹

10
在没有2个SD卡的设备上,此操作将失败。假定第一个发现是内部的,第二个发现是外部的...
Caner

没有工作了在Nexus 5和Nexus 7,通过OTG线连接的USB设备
Khawar拉扎

4
/system/etc/vold.fstab在android 4.3+中无法访问
Ali

37

我有一个使用了 ListPreference要求用户选择他们想要保存内容的位置的位置。

在该应用程序,我扫描/proc/mounts/system/etc/vold.fstab为SD卡安装点。我将每个文件的挂载点存储在两个单独的文件中ArrayList s中。

然后,我将一个列表与另一个列表进行了比较,并丢弃了两个列表中都不存在的项目。这给了我每个sdcard的根路径列表。

从那里,我测试了路径File.exists()File.isDirectory()File.canWrite()。如果这些测试中有任何一个为假,我将从列表中删除该路径。

无论列表中剩下什么,我都会转换为String[]数组,以便供ListPreference values属性。

您可以在此处查看代码:http : //sapienmobile.com/?p=204


仅供参考,这不适用于Galaxy S3、2张SD卡,而vold.conf中仅列出了一张
3c71 2012年

1
@ 3c71-您能给我发送Galaxy S3的vold并挂载文件吗?我将调整代码以覆盖它。
男爵

Galaxy S,发现的所有路径都不可写,很奇怪。找到了两个存储,默认的/ mnt / sdcard和/ storage / sdcard0都无法测试
5

1
我调整了代码以忽略mounts文件。这就是摩托罗拉和三星设备上的问题。mounts文件没有涵盖external_sd的情况,但是它在vold中列出。我班上的最初版本将坐骑与vold和废弃的东西进行了比较,而这两种都不常见。从上面的同一链接中获取更新的类。
男爵

1
谢谢男爵,这是“答案”。至少是有用的。
pstoppani

23

您可以尝试使用称为ContextCompat.getExternalFilesDirs()的支持库函数:

      final File[] appsDir=ContextCompat.getExternalFilesDirs(getActivity(),null);
      final ArrayList<File> extRootPaths=new ArrayList<>();
      for(final File file : appsDir)
        extRootPaths.add(file.getParentFile().getParentFile().getParentFile().getParentFile());

第一个是主要的外部存储,其余的应该是真实的SD卡路径。

多个“ .getParentFile()”的原因是要转到另一个文件夹,因为原始路径是

.../Android/data/YOUR_APP_PACKAGE_NAME/files/

编辑:这是我创建的一种更全面的方法,用于获取sd卡路径:

  /**
   * 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<String> 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<String> result=new ArrayList<>();
    if(includePrimaryExternalStorage||externalCacheDirs.length==1)
      result.add(getRootOfInnerSdCardFolder(externalCacheDirs[0]));
    for(int i=1;i<externalCacheDirs.length;++i)
      {
      final File file=externalCacheDirs[i];
      if(file==null)
        continue;
      final String storageState=EnvironmentCompat.getStorageState(file);
      if(Environment.MEDIA_MOUNTED.equals(storageState))
        result.add(getRootOfInnerSdCardFolder(externalCacheDirs[i]));
      }
    if(result.isEmpty())
      return null;
    return result;
    }

  /** Given any file/folder inside an sd card, this will return the path of the sd card */
  private static String getRootOfInnerSdCardFolder(File file)
    {
    if(file==null)
      return null;
    final long totalSpace=file.getTotalSpace();
    while(true)
      {
      final File parentFile=file.getParentFile();
      if(parentFile==null||parentFile.getTotalSpace()!=totalSpace||!parentFile.canRead())
        return file.getAbsolutePath();
      file=parentFile;
      }
    }

这似乎是一个很好的答案,但是如何将其整合到一个简单的活动中呢?有几个变量没有定义,像AppContextCompactEnvironmentCompact
安东尼

通过支持库可以使用@Antonio ContextCompact,EnvironmentCompact。“ App.global()”只是应用程序上下文,由于我不喜欢到处添加Context参数,因此已对其进行全局设置。
Android开发人员

1
大!适用于我的v4.4三星GT S Advance防雷设备,希望它将对其他设备
起作用

@androiddeveloper编辑后的答案适用于所有设备和SD卡大小吗?
Rahulrr2602 '17

1
这对我来说非常有效-应该是公认的答案。
悖论

17

为了检索所有外部存储(无论是SD卡还是内部不可移动存储),可以使用以下代码:

final String state = Environment.getExternalStorageState();

if ( Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) ) {  // we can read the External Storage...           
    //Retrieve the primary External Storage:
    final File primaryExternalStorage = Environment.getExternalStorageDirectory();

    //Retrieve the External Storages root directory:
    final String externalStorageRootDir;
    if ( (externalStorageRootDir = primaryExternalStorage.getParent()) == null ) {  // no parent...
        Log.d(TAG, "External Storage: " + primaryExternalStorage + "\n");
    }
    else {
        final File externalStorageRoot = new File( externalStorageRootDir );
        final File[] files = externalStorageRoot.listFiles();

        for ( final File file : files ) {
            if ( file.isDirectory() && file.canRead() && (file.listFiles().length > 0) ) {  // it is a real directory (not a USB drive)...
                Log.d(TAG, "External Storage: " + file.getAbsolutePath() + "\n");
            }
        }
    }
}

或者,您可以使用System.getenv(“ EXTERNAL_STORAGE”)检索主外部存储目录(例如“ / storage / sdcard0”)和System.getenv(“ SECONDARY_STORAGE”)来检索所有辅助目录的列表(例如“ / storage / extSdCard:/ storage / UsbDriveA:/ storage / UsbDriveB“)。请记住,同样在这种情况下,您可能希望过滤辅助目录列表以排除USB驱动器。

无论如何,请注意,使用硬编码路径始终是一种不好的方法(尤其是当每个制造商都可以根据需要更改时)。


2
只要考虑任何不发表评论的不愿发表评论的人,那么我就对此进行了弥补。;)但是,我想您的方法相当随意:我们如何知道跳过那些“ USB驱动器”,而实际上保留所有其他内容等同于“ sdcards”,正如问题所问的那样?您的建议System.getenv("SECONDARY_STORAGE")也可以使用一些参考,因为它似乎没有记录。
Sz。

1
据我所知,在Android API中没有引用标准方法来检索所有外部存储。但是,提出的方法根本不是任意的。在Android中,就像在每个Unix / Linux系统中一样,所有安装的存储设备都存储/链接在一个公共目录中:“ / mnt”(用于安装存储设备的标准Unix / Linux目录),或者在最新版本中,是“ /存储”。这就是为什么您可以确定在此文件夹中找到所有链接的SD卡的原因。
Paolo Rovelli 2014年

1
关于System.getenv(“ EXTERNAL_STORAGE”)方法,我没有任何引用,但没有API页面(没有太多解释):developer.android.com/reference/java/lang/…我找不到任何Android系统环境变量的官方页面。但是,您可以在此处找到它们的简短列表:herongyang.com/Android/…–
Paolo Rovelli

我不确定sdcard的意思是,/mnt里面还会有其他各种fs树,而不仅仅是SD卡和USB驱动器。如果我理解正确的话,您的代码还会列出所有内部(甚至虚拟的)文件系统挂载,而问题只需要 sdcards即可。
Sz。

1
我懂了。是的,你是对的。使用我的方法,您还将检索内部(不可移动)SD存储器。
Paolo Rovelli 2014年

15

像Richard一样,我也使用/ proc / mounts文件来获取可用存储选项的列表

public class StorageUtils {

    private static final String TAG = "StorageUtils";

    public static class StorageInfo {

        public final String path;
        public final boolean internal;
        public final boolean readonly;
        public final int display_number;

        StorageInfo(String path, boolean internal, boolean readonly, int display_number) {
            this.path = path;
            this.internal = internal;
            this.readonly = readonly;
            this.display_number = display_number;
        }

        public String getDisplayName() {
            StringBuilder res = new StringBuilder();
            if (internal) {
                res.append("Internal SD card");
            } else if (display_number > 1) {
                res.append("SD card " + display_number);
            } else {
                res.append("SD card");
            }
            if (readonly) {
                res.append(" (Read only)");
            }
            return res.toString();
        }
    }

    public static List<StorageInfo> getStorageList() {

        List<StorageInfo> list = new ArrayList<StorageInfo>();
        String def_path = Environment.getExternalStorageDirectory().getPath();
        boolean def_path_internal = !Environment.isExternalStorageRemovable();
        String def_path_state = Environment.getExternalStorageState();
        boolean def_path_available = def_path_state.equals(Environment.MEDIA_MOUNTED)
                                    || def_path_state.equals(Environment.MEDIA_MOUNTED_READ_ONLY);
        boolean def_path_readonly = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY);
        BufferedReader buf_reader = null;
        try {
            HashSet<String> paths = new HashSet<String>();
            buf_reader = new BufferedReader(new FileReader("/proc/mounts"));
            String line;
            int cur_display_number = 1;
            Log.d(TAG, "/proc/mounts");
            while ((line = buf_reader.readLine()) != null) {
                Log.d(TAG, line);
                if (line.contains("vfat") || line.contains("/mnt")) {
                    StringTokenizer tokens = new StringTokenizer(line, " ");
                    String unused = tokens.nextToken(); //device
                    String mount_point = tokens.nextToken(); //mount point
                    if (paths.contains(mount_point)) {
                        continue;
                    }
                    unused = tokens.nextToken(); //file system
                    List<String> flags = Arrays.asList(tokens.nextToken().split(",")); //flags
                    boolean readonly = flags.contains("ro");

                    if (mount_point.equals(def_path)) {
                        paths.add(def_path);
                        list.add(0, new StorageInfo(def_path, def_path_internal, readonly, -1));
                    } else if (line.contains("/dev/block/vold")) {
                        if (!line.contains("/mnt/secure")
                            && !line.contains("/mnt/asec")
                            && !line.contains("/mnt/obb")
                            && !line.contains("/dev/mapper")
                            && !line.contains("tmpfs")) {
                            paths.add(mount_point);
                            list.add(new StorageInfo(mount_point, false, readonly, cur_display_number++));
                        }
                    }
                }
            }

            if (!paths.contains(def_path) && def_path_available) {
                list.add(0, new StorageInfo(def_path, def_path_internal, def_path_readonly, -1));
            }

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (buf_reader != null) {
                try {
                    buf_reader.close();
                } catch (IOException ex) {}
            }
        }
        return list;
    }    
}

谢谢。工作完美。我喜欢您使StorageInfo不可更改的方式。另一方面printStackTrace?什么时候有android.util.Log.e
马丁

1
没有工作了在Nexus 5和Nexus 7,通过OTG线连接的USB设备
Khawar拉扎

1
我不能在SD卡用这个来写文件
铕VID

与@EuVid相同的问题适用于VM / AVD,但不适用于硬件
间谍

11

通过读取/proc/mounts(标准Linux文件)并对照vold数据(/system/etc/vold.conf)进行交叉检查,可以找到其他SD卡的安装位置。请注意,返回的位置Environment.getExternalStorageDirectory()可能不会出现在vold配置中(在某些设备中,它是无法卸载的内部存储器),但仍必须包含在列表中。但是,我们找不到向用户描述它们的好方法。


Imo,使用mount比读取/proc文件系统更兼容。问题是SD卡没有必要格式化为FAT。另外,卡的安装点可能因ROM而异。另外,可能还有其他几个VFAT分区...
borisstr 2011年

1
@borisstr:嗯,实际上Android使用的是vold,因此也应该查看它的配置。
1月Hudec

我在上面的文章中分享的代码文件包括一种描述发现的用户根路径的方法。查看setProperties()方法。
男爵

1
@borisstr,实际上,不,读取/ proc / mounts在Android设备上比启动mount可执行文件更可移植,尤其是不鼓励启动可执行文件。
克里斯·斯特拉顿

7

这次,我将尝试该主题内的所有解决方案。但是,它们都不能在带有一张外部(可移动)和一张内部(不可移动)卡的设备上正常工作。无法从'mount'命令,'proc / mounts'文件等获取外部卡的路径。

我创建自己的解决方案(在Paulo Luan的基础上):

String sSDpath = null;
File   fileCur = null;
for( String sPathCur : Arrays.asList( "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;
   }
}
fileCur = null;
if( sSDpath == null)  sSDpath = Environment.getExternalStorageDirectory().getAbsolutePath();

6

如果您查看源代码,android.os.Environment将会看到Android严重依赖于环境变量作为路径。您可以使用“ SECONDARY_STORAGE”环境变量来找到可移动sd卡的路径。

/**
 * Get a file using an environmental variable.
 *
 * @param variableName
 *         The Environment variable name.
 * @param paths
 *         Any paths to the file if the Environment variable was not found.
 * @return the File or {@code null} if the File could not be located.
 */
private static File getDirectory(String variableName, String... paths) {
    String path = System.getenv(variableName);
    if (!TextUtils.isEmpty(path)) {
        if (path.contains(":")) {
            for (String _path : path.split(":")) {
                File file = new File(_path);
                if (file.exists()) {
                    return file;
                }
            }
        } else {
            File file = new File(path);
            if (file.exists()) {
                return file;
            }
        }
    }
    if (paths != null && paths.length > 0) {
        for (String _path : paths) {
            File file = new File(_path);
            if (file.exists()) {
                return file;
            }
        }
    }
    return null;
}

用法示例:

public static final File REMOVABLE_STORAGE = getDirectory("SECONDARY_STORAGE");

5

只需使用以下命令:

String primary_sd = System.getenv("EXTERNAL_STORAGE");
if(primary_sd != null)
    Log.i("EXTERNAL_STORAGE", primary_sd);
String secondary_sd = System.getenv("SECONDARY_STORAGE");
if(secondary_sd != null)
    Log.i("SECONDARY_STORAGE", secondary_sd)

在某些设备上,SECONDARY_STORAGE有多个路径用冒号(“:”)分隔。这就是为什么我拆分字符串的原因(请参见上面的答案)。
Jared Rummler

这些都为我返回null。
蒂姆·库珀

5

有通用的方法来查找外部SD卡的位置吗?

通过普遍的方式,如果你的意思是官方的方法; 是的,有一个。

在API级别19中,即在Android 4.4版Kitkat中,他们添加File[] getExternalFilesDirs (String type)Context Class中允许应用程序将数据/文件存储在micro SD卡中的类。

Android 4.4是该平台的第一个版本,实际上已允许应用程序使用SD卡进行存储。在API级别19之前,对SD卡的任何访问都是通过不支持的私有API进行的。

getExternalFilesDirs(String type)返回所有共享/外部存储设备上应用程序特定目录的绝对路径。这意味着它将返回到内部和外部存储器的路径。通常,第二条返回路径将是microSD卡(如果有)的存储路径。

但请注意,

共享存储可能并不总是可用,因为用户可以弹出可移动媒体。可以使用检查媒体状态 getExternalStorageState(File)

这些文件没有强制实施安全性。例如,任何持有的应用程序WRITE_EXTERNAL_STORAGE都可以写入这些文件。

根据Google / Android官方文档的内部和外部存储术语与我们所认为的完全不同


“根据Google / Android官方文档的内部和外部存储术语与我们所认为的完全不同。” 是的,事实上,问题的标题明确说明了OP正在询问可移动 SD卡。getExternalFilesDirs()通常会返回不可移动的SD卡,因此不能,这不是查找可移动SD卡位置的通用方法。
LarsH

“ getExternalFilesDirs(String type)返回所有共享/外部存储设备上应用程序特定目录的绝对路径。这意味着它将同时返回内部和外部存储器的路径。” 这对句子非常容易引起误解,因为为了使它们都正确,“外部”必须表示两种不同且相互矛盾的事物。
LarsH

4

这是我用来查找外部卡的方法。使用mount cmd return然后解析vfat部分。

String s = "";
try {
Process process = new ProcessBuilder().command("mount")
        .redirectErrorStream(true).start();

process.waitFor();

InputStream is = process.getInputStream();
byte[] buffer = new byte[1024];
while (is.read(buffer) != -1) {
    s = s + new String(buffer);
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}

//用行分隔mount列表
String[] lines = s.split("\n");
for(int i=0; i<lines.length; i++) {
//如果行内有挂载路径且为vfat类型,说明可能是内置或者外置sd的挂载点
if(-1 != lines[i].indexOf(path[0]) && -1 != lines[i].indexOf("vfat")) {
    //再用空格分隔
    String[] blocks = lines[i].split("\\s");
    for(int j=0; j<blocks.length; j++) {
        //判断是否是挂载为vfat类型
        if(-1 != blocks[j].indexOf(path[0])) {
            //Test if it is the external sd card.
        }
    }
}
}

4

该解决方案处理了以下事实: System.getenv("SECONDARY_STORAGE")棉花糖没有用。

经过测试并致力于:

  • 三星Galaxy Tab 2(Android 4.1.1-股票)
  • 三星Galaxy Note 8.0(Android 4.2.2-库存)
  • 三星Galaxy S4(Android 4.4-股票)
  • 三星Galaxy S4(Android 5.1.1-Cyanogenmod)
  • 三星Galaxy Tab A(Android 6.0.1-股票)

    /**
     * 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;
    }

2

自从以上给出我最初的回答以来,扫描伏特在各个制造商中都不再可行。

我开发了一种更可靠,更直接的方法。

File mnt = new File("/storage");
if (!mnt.exists())
    mnt = new File("/mnt");

File[] roots = mnt.listFiles(new FileFilter() {

    @Override
    public boolean accept(File pathname) {
        return pathname.isDirectory() && pathname.exists()
                && pathname.canWrite() && !pathname.isHidden()
                && !isSymlink(pathname);
    }
});

根目录将包含系统上所有可写的根目录,包括任何与USB连接的USB设备。

注意:canWrite方法需要android.permission.WRITE_EXTERNAL_STORAGE权限。


未为类型new FileFilter(){}定义方法isSymlink(File)
Omid Omidi 2015年

是否知道由于canWrite在Android 4.4上找不到外部SD卡?
安东尼

这肯定比其他方法简单明了,但是可靠吗?例如,我在某些三星设备上读到的/external_sd是外部microSD卡;一些LG的,它的/_ExternalSD; 在某些设备上是/sdcard。也许后者是/storage/sdcard0或与之类似的符号链接,但是这些其他元素是否真的会被/storage/*和可靠地覆盖/mount/*
LarsH 2015年

另外,是否有必要使用pathname.canWrite()并要求WRITE_EXTERNAL_STORAGE权限?为什么不打电话pathname.canRead()呢?
LarsH 2015年

1

太晚了,但最后我得到了一些我测试过的大多数设备(按制造商和android版本)的信息,它在Android 2.2+上可以正常工作。如果发现它不起作用,请用您的设备名称对其进行注释。我会修好它。如果有人感兴趣,我会解释它是如何工作的。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;

import android.util.Log;


/**
 * @author ajeet
 *05-Dec-2014  2014
 *
 */
public class StorageUtil {

    public boolean isRemovebleSDCardMounted() {
        File file = new File("/sys/class/block/");
        File[] files = file.listFiles(new MmcblkFilter("mmcblk\\d$"));
        boolean flag = false;
        for (File mmcfile : files) {
            File scrfile = new File(mmcfile, "device/scr");
            if (scrfile.exists()) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    public String getRemovebleSDCardPath() throws IOException {
        String sdpath = null;
        File file = new File("/sys/class/block/");
        File[] files = file.listFiles(new MmcblkFilter("mmcblk\\d$"));
        String sdcardDevfile = null;
        for (File mmcfile : files) {
            Log.d("SDCARD", mmcfile.getAbsolutePath());
            File scrfile = new File(mmcfile, "device/scr");
            if (scrfile.exists()) {
                sdcardDevfile = mmcfile.getName();
                Log.d("SDCARD", mmcfile.getName());
                break;
            }
        }
        if (sdcardDevfile == null) {
            return null;
        }
        FileInputStream is;
        BufferedReader reader;

        files = file.listFiles(new MmcblkFilter(sdcardDevfile + "p\\d+"));
        String deviceName = null;
        if (files.length > 0) {
            Log.d("SDCARD", files[0].getAbsolutePath());
            File devfile = new File(files[0], "dev");
            if (devfile.exists()) {
                FileInputStream fis = new FileInputStream(devfile);
                reader = new BufferedReader(new InputStreamReader(fis));
                String line = reader.readLine();
                deviceName = line;
            }
            Log.d("SDCARD", "" + deviceName);
            if (deviceName == null) {
                return null;
            }
            Log.d("SDCARD", deviceName);

            final File mountFile = new File("/proc/self/mountinfo");

            if (mountFile.exists()) {
                is = new FileInputStream(mountFile);
                reader = new BufferedReader(new InputStreamReader(is));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    // Log.d("SDCARD", line);
                    // line = reader.readLine();
                    // Log.d("SDCARD", line);
                    String[] mPonts = line.split("\\s+");
                    if (mPonts.length > 6) {
                        if (mPonts[2].trim().equalsIgnoreCase(deviceName)) {
                            if (mPonts[4].contains(".android_secure")
                                    || mPonts[4].contains("asec")) {
                                continue;
                            }
                            sdpath = mPonts[4];
                            Log.d("SDCARD", mPonts[4]);

                        }
                    }

                }
            }

        }

        return sdpath;
    }

    static class MmcblkFilter implements FilenameFilter {
        private String pattern;

        public MmcblkFilter(String pattern) {
            this.pattern = pattern;

        }

        @Override
        public boolean accept(File dir, String filename) {
            if (filename.matches(pattern)) {
                return true;
            }
            return false;
        }

    }

}

嘿唐纳德,请先尝试。如果它不起作用,请评论您的设备。我们在Android
2.2

您的课程为我提供了Samsung Galaxy S4,GT-I9500,Android 5.0.1(未植根设备)上的/ mnt / media_rw / extSdCard。但是使用ES File Manager在/ mnt / media_rw文件夹中看不到任何内容……
2015年

@isabsent使用if(Build.VERSION.SDK_INT> = Build.VERSION_CODES.KITKAT){File [] file = context.getExternalFilesDirs(null); 返回file.length> 1?file [1]:null; }
Ajeet47'4-4-24

您如何看待stackoverflow.com/a/27197248/753575?对于Build.VERSION.SDK_INT> = Build.VERSION_CODES.KITKAT而言,这种方法是否更全面?
2015年

是。它可以确保您从context.getExternalFilesDirs(null)获得有意义的路径,但已将其修剪为根路径(它将返回您应用程序目录的路径。在“ / Android”上修剪它)
Ajeet47 2015年

1

通过编写以下代码,您将获得位置:

/ storage / 663D-554E / Android / data / app_package_name / files /

它将您的应用程序数据存储在sd_card内的/ android / data位置。

File[] list = ContextCompat.getExternalFilesDirs(MainActivity.this, null);

list[1]+"/fol" 

用于获取位置,将0传递给内部,将1传递给sdcard到文件数组。

我已经在moto g4 plus和Samsung设备上测试了此代码(一切正常)。

希望这会有所帮助。


有时sd卡路径不在索引1上,我已经看到了它在索引0上的情况。最好遵循其他方法
Raghav Satyadev

1

这是我用来查找可移动 SD卡的方法。它很复杂,在某些情况下可能有些过头,但是我在最近几年测试过的各种Android版本和设备制造商中都可以使用。我不知道任何设备,因为如果安装了API级别15,则在该设备上找不到SD卡。在大多数情况下,它不会返回误报,尤其是当您给它提供要查找的已知文件的名称时。

如果遇到任何无法使用的情况,请告诉我。

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.regex.Pattern;

public class SDCard {
    private static final String TAG = "SDCard";

    /** In some scenarios we can expect to find a specified file or folder on SD cards designed
     * to work with this app. If so, set KNOWNFILE to that filename. It will make our job easier.
     * Set it to null otherwise. */
    private static final String KNOWNFILE = null;

    /** Common paths for microSD card. **/
    private static String[] commonPaths = {
            // Some of these taken from
            // /programming/13976982/removable-storage-external-sdcard-path-by-manufacturers
            // These are roughly in order such that the earlier ones, if they exist, are more sure
            // to be removable storage than the later ones.
            "/mnt/Removable/MicroSD",
            "/storage/removable/sdcard1", // !< Sony Xperia Z1
            "/Removable/MicroSD", // Asus ZenPad C
            "/removable/microsd",
            "/external_sd", // Samsung
            "/_ExternalSD", // some LGs
            "/storage/extSdCard", // later Samsung
            "/storage/extsdcard", // Main filesystem is case-sensitive; FAT isn't.
            "/mnt/extsd", // some Chinese tablets, e.g. Zeki
            "/storage/sdcard1", // If this exists it's more likely than sdcard0 to be removable.
            "/mnt/extSdCard",
            "/mnt/sdcard/external_sd",
            "/mnt/external_sd",
            "/storage/external_SD",
            "/storage/ext_sd", // HTC One Max
            "/mnt/sdcard/_ExternalSD",
            "/mnt/sdcard-ext",

            "/sdcard2", // HTC One M8s
            "/sdcard1", // Sony Xperia Z
            "/mnt/media_rw/sdcard1",   // 4.4.2 on CyanogenMod S3
            "/mnt/sdcard", // This can be built-in storage (non-removable).
            "/sdcard",
            "/storage/sdcard0",
            "/emmc",
            "/mnt/emmc",
            "/sdcard/sd",
            "/mnt/sdcard/bpemmctest",
            "/mnt/external1",
            "/data/sdext4",
            "/data/sdext3",
            "/data/sdext2",
            "/data/sdext",
            "/storage/microsd" //ASUS ZenFone 2

            // If we ever decide to support USB OTG storage, the following paths could be helpful:
            // An LG Nexus 5 apparently uses usb://1002/UsbStorage/ as a URI to access an SD
            // card over OTG cable. Other models, like Galaxy S5, use /storage/UsbDriveA
            //        "/mnt/usb_storage",
            //        "/mnt/UsbDriveA",
            //        "/mnt/UsbDriveB",
    };

    /** Find path to removable SD card. */
    public static File findSdCardPath(Context context) {
        String[] mountFields;
        BufferedReader bufferedReader = null;
        String lineRead = null;

        /** Possible SD card paths */
        LinkedHashSet<File> candidatePaths = new LinkedHashSet<>();

        /** Build a list of candidate paths, roughly in order of preference. That way if
         * we can't definitively detect removable storage, we at least can pick a more likely
         * candidate. */

        // Could do: use getExternalStorageState(File path), with and without an argument, when
        // available. With an argument is available since API level 21.
        // This may not be necessary, since we also check whether a directory exists and has contents,
        // which would fail if the external storage state is neither MOUNTED nor MOUNTED_READ_ONLY.

        // I moved hard-coded paths toward the end, but we need to make sure we put the ones in
        // backwards order that are returned by the OS. And make sure the iterators respect
        // the order!
        // This is because when multiple "external" storage paths are returned, it's always (in
        // experience, but not guaranteed by documentation) with internal/emulated storage
        // first, removable storage second.

        // Add value of environment variables as candidates, if set:
        // EXTERNAL_STORAGE, SECONDARY_STORAGE, EXTERNAL_SDCARD_STORAGE
        // But note they are *not* necessarily *removable* storage! Especially EXTERNAL_STORAGE.
        // And they are not documented (API) features. Typically useful only for old versions of Android.

        String val = System.getenv("SECONDARY_STORAGE");
        if (!TextUtils.isEmpty(val)) addPath(val, null, candidatePaths);
        val = System.getenv("EXTERNAL_SDCARD_STORAGE");
        if (!TextUtils.isEmpty(val)) addPath(val, null, candidatePaths);

        // Get listing of mounted devices with their properties.
        ArrayList<File> mountedPaths = new ArrayList<>();
        try {
            // Note: Despite restricting some access to /proc (http://stackoverflow.com/a/38728738/423105),
            // Android 7.0 does *not* block access to /proc/mounts, according to our test on George's Alcatel A30 GSM.
            bufferedReader = new BufferedReader(new FileReader("/proc/mounts"));

            // Iterate over each line of the mounts listing.
            while ((lineRead = bufferedReader.readLine()) != null) {
                Log.d(TAG, "\nMounts line: " + lineRead);
                mountFields = lineRead.split(" ");

                // columns: device, mountpoint, fs type, options... Example:
                // /dev/block/vold/179:97 /storage/sdcard1 vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1000,gid=1015,fmask=0002,dmask=0002,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
                String device = mountFields[0], path = mountFields[1], fsType = mountFields[2];

                // The device, path, and fs type must conform to expected patterns.
                if (!(devicePattern.matcher(device).matches() &&
                        pathPattern.matcher(path).matches() &&
                        fsTypePattern.matcher(fsType).matches()) ||
                        // mtdblock is internal, I'm told.
                        device.contains("mtdblock") ||
                        // Check for disqualifying patterns in the path.
                        pathAntiPattern.matcher(path).matches()) {
                    // If this mounts line fails our tests, skip it.
                    continue;
                }

                // TODO maybe: check options to make sure it's mounted RW?
                // The answer at http://stackoverflow.com/a/13648873/423105 does.
                // But it hasn't seemed to be necessary so far in my testing.

                // This line met the criteria so far, so add it to candidate list.
                addPath(path, null, mountedPaths);
            }
        } catch (IOException ignored) {
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException ignored) {
                }
            }
        }

        // Append the paths from mount table to candidate list, in reverse order.
        if (!mountedPaths.isEmpty()) {
            // See https://stackoverflow.com/a/5374346/423105 on why the following is necessary.
            // Basically, .toArray() needs its parameter to know what type of array to return.
            File[] mountedPathsArray = mountedPaths.toArray(new File[mountedPaths.size()]);
            addAncestors(candidatePaths, mountedPathsArray);
        }

        // Add hard-coded known common paths to candidate list:
        addStrings(candidatePaths, commonPaths);

        // If the above doesn't work we could try the following other options, but in my experience they
        // haven't added anything helpful yet.

        // getExternalFilesDir() and getExternalStorageDirectory() typically something app-specific like
        //   /storage/sdcard1/Android/data/com.mybackuparchives.android/files
        // so we want the great-great-grandparent folder.

        // This may be non-removable.
        Log.d(TAG, "Environment.getExternalStorageDirectory():");
        addPath(null, ancestor(Environment.getExternalStorageDirectory()), candidatePaths);

        // Context.getExternalFilesDirs() is only available from API level 19. You can use
        // ContextCompat.getExternalFilesDirs() on earlier APIs, but it only returns one dir anyway.
        Log.d(TAG, "context.getExternalFilesDir(null):");
        addPath(null, ancestor(context.getExternalFilesDir(null)), candidatePaths);

        // "Returns absolute paths to application-specific directories on all external storage
        // devices where the application can place persistent files it owns."
        // We might be able to use these to deduce a higher-level folder that isn't app-specific.
        // Also, we apparently have to call getExternalFilesDir[s](), at least in KITKAT+, in order to ensure that the
        // "external files" directory exists and is available.
        Log.d(TAG, "ContextCompat.getExternalFilesDirs(context, null):");
        addAncestors(candidatePaths, ContextCompat.getExternalFilesDirs(context, null));
        // Very similar results:
        Log.d(TAG, "ContextCompat.getExternalCacheDirs(context):");
        addAncestors(candidatePaths, ContextCompat.getExternalCacheDirs(context));

        // TODO maybe: use getExternalStorageState(File path), with and without an argument, when
        // available. With an argument is available since API level 21.
        // This may not be necessary, since we also check whether a directory exists,
        // which would fail if the external storage state is neither MOUNTED nor MOUNTED_READ_ONLY.

        // A "public" external storage directory. But in my experience it doesn't add anything helpful.
        // Note that you can't pass null, or you'll get an NPE.
        final File publicDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
        // Take the parent, because we tend to get a path like /pathTo/sdCard/Music.
        addPath(null, publicDirectory.getParentFile(), candidatePaths);
        // EXTERNAL_STORAGE: may not be removable.
        val = System.getenv("EXTERNAL_STORAGE");
        if (!TextUtils.isEmpty(val)) addPath(val, null, candidatePaths);

        if (candidatePaths.isEmpty()) {
            Log.w(TAG, "No removable microSD card found.");
            return null;
        } else {
            Log.i(TAG, "\nFound potential removable storage locations: " + candidatePaths);
        }

        // Accept or eliminate candidate paths if we can determine whether they're removable storage.
        // In Lollipop and later, we can check isExternalStorageRemovable() status on each candidate.
        if (Build.VERSION.SDK_INT >= 21) {
            Iterator<File> itf = candidatePaths.iterator();
            while (itf.hasNext()) {
                File dir = itf.next();
                // handle illegalArgumentException if the path is not a valid storage device.
                try {
                    if (Environment.isExternalStorageRemovable(dir)
                        // && containsKnownFile(dir)
                            ) {
                        Log.i(TAG, dir.getPath() + " is removable external storage");
                        return dir;
                    } else if (Environment.isExternalStorageEmulated(dir)) {
                        Log.d(TAG, "Removing emulated external storage dir " + dir);
                        itf.remove();
                    }
                } catch (IllegalArgumentException e) {
                    Log.d(TAG, "isRemovable(" + dir.getPath() + "): not a valid storage device.", e);
                }
            }
        }

        // Continue trying to accept or eliminate candidate paths based on whether they're removable storage.
        // On pre-Lollipop, we only have singular externalStorage. Check whether it's removable.
        if (Build.VERSION.SDK_INT >= 9) {
            File externalStorage = Environment.getExternalStorageDirectory();
            Log.d(TAG, String.format(Locale.ROOT, "findSDCardPath: getExternalStorageDirectory = %s", externalStorage.getPath()));
            if (Environment.isExternalStorageRemovable()) {
                // Make sure this is a candidate.
                // TODO: Does this contains() work? Should we be canonicalizing paths before comparing?
                if (candidatePaths.contains(externalStorage)
                    // && containsKnownFile(externalStorage)
                        ) {
                    Log.d(TAG, "Using externalStorage dir " + externalStorage);
                    return externalStorage;
                }
            } else if (Build.VERSION.SDK_INT >= 11 && Environment.isExternalStorageEmulated()) {
                Log.d(TAG, "Removing emulated external storage dir " + externalStorage);
                candidatePaths.remove(externalStorage);
            }
        }

        // If any directory contains our special test file, consider that the microSD card.
        if (KNOWNFILE != null) {
            for (File dir : candidatePaths) {
                Log.d(TAG, String.format(Locale.ROOT, "findSdCardPath: Looking for known file in candidate path, %s", dir));
                if (containsKnownFile(dir)) return dir;
            }
        }

        // If we don't find the known file, still try taking the first candidate.
        if (!candidatePaths.isEmpty()) {
            Log.d(TAG, "No definitive path to SD card; taking the first realistic candidate.");
            return candidatePaths.iterator().next();
        }

        // If no reasonable path was found, give up.
        return null;
    }

    /** Add each path to the collection. */
    private static void addStrings(LinkedHashSet<File> candidatePaths, String[] newPaths) {
        for (String path : newPaths) {
            addPath(path, null, candidatePaths);
        }
    }

    /** Add ancestor of each File to the collection. */
    private static void addAncestors(LinkedHashSet<File> candidatePaths, File[] files) {
        for (int i = files.length - 1; i >= 0; i--) {
            addPath(null, ancestor(files[i]), candidatePaths);
        }
    }

    /**
     * Add a new candidate directory path to our list, if it's not obviously wrong.
     * Supply path as either String or File object.
     * @param strNew path of directory to add (or null)
     * @param fileNew directory to add (or null)
     */
    private static void addPath(String strNew, File fileNew, Collection<File> paths) {
        // If one of the arguments is null, fill it in from the other.
        if (strNew == null) {
            if (fileNew == null) return;
            strNew = fileNew.getPath();
        } else if (fileNew == null) {
            fileNew = new File(strNew);
        }

        if (!paths.contains(fileNew) &&
                // Check for paths known not to be removable SD card.
                // The antipattern check can be redundant, depending on where this is called from.
                !pathAntiPattern.matcher(strNew).matches()) {

            // Eliminate candidate if not a directory or not fully accessible.
            if (fileNew.exists() && fileNew.isDirectory() && fileNew.canExecute()) {
                Log.d(TAG, "  Adding candidate path " + strNew);
                paths.add(fileNew);
            } else {
                Log.d(TAG, String.format(Locale.ROOT, "  Invalid path %s: exists: %b isDir: %b canExec: %b canRead: %b",
                        strNew, fileNew.exists(), fileNew.isDirectory(), fileNew.canExecute(), fileNew.canRead()));
            }
        }
    }

    private static final String ANDROID_DIR = File.separator + "Android";

    private static File ancestor(File dir) {
        // getExternalFilesDir() and getExternalStorageDirectory() typically something app-specific like
        //   /storage/sdcard1/Android/data/com.mybackuparchives.android/files
        // so we want the great-great-grandparent folder.
        if (dir == null) {
            return null;
        } else {
            String path = dir.getAbsolutePath();
            int i = path.indexOf(ANDROID_DIR);
            if (i == -1) {
                return dir;
            } else {
                return new File(path.substring(0, i));
            }
        }
    }

    /** Returns true iff dir contains the special test file.
     * Assumes that dir exists and is a directory. (Is this a necessary assumption?) */
    private static boolean containsKnownFile(File dir) {
        if (KNOWNFILE == null) return false;

        File knownFile = new File(dir, KNOWNFILE);
        return knownFile.exists();
    }

    private static Pattern
            /** Pattern that SD card device should match */
            devicePattern = Pattern.compile("/dev/(block/.*vold.*|fuse)|/mnt/.*"),
    /** Pattern that SD card mount path should match */
    pathPattern = Pattern.compile("/(mnt|storage|external_sd|extsd|_ExternalSD|Removable|.*MicroSD).*",
            Pattern.CASE_INSENSITIVE),
    /** Pattern that the mount path should not match.
     * 'emulated' indicates an internal storage location, so skip it.
     * 'asec' is an encrypted package file, decrypted and mounted as a directory. */
    pathAntiPattern = Pattern.compile(".*(/secure|/asec|/emulated).*"),
    /** These are expected fs types, including vfat. tmpfs is not OK.
     * fuse can be removable SD card (as on Moto E or Asus ZenPad), or can be internal (Huawei G610). */
    fsTypePattern = Pattern.compile(".*(fat|msdos|ntfs|ext[34]|fuse|sdcard|esdfs).*");
}

聚苯乙烯

  • 不要忘<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />了清单。在API级别23或更高版本中,请确保使用checkSelfPermission/ requestPermissions
  • 如果您希望在SD卡上找到文件或文件夹,请设置KNOWNFILE =“ myappfile”。这使得检测更加准确。
  • 显然,您要缓存的值 findSdCardPath(),而不是每次需要时都重新计算它。
  • Log.d()上面的代码中有一堆日志()。它可以帮助诊断找不到正确路径的任何情况。如果您不想登录,请注释掉。

拒绝投票的人,您能提出改善此答案的方法吗?
LarsH

1

我发现的唯一可行的解​​决方案是使用反射的解决方案

 /**
 * Get external sd card path using reflection
 * @param mContext
 * @param is_removable is external storage removable
 * @return
 */
private static String getExternalStoragePath(Context mContext, boolean is_removable) {

    StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
    Class<?> storageVolumeClazz = null;
    try {
        storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
        Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
        Method getPath = storageVolumeClazz.getMethod("getPath");
        Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
        Object result = getVolumeList.invoke(mStorageManager);
        final int length = Array.getLength(result);
        for (int i = 0; i < length; i++) {
            Object storageVolumeElement = Array.get(result, i);
            String path = (String) getPath.invoke(storageVolumeElement);
            boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
            if (is_removable == removable) {
                return path;
            }
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
}

我个人不喜欢使用反射,因为Google不欣赏Android新版本中的向后兼容性!
Behrouz.M

0

我不知道为什么,但是在使用它之前,我需要在公共存储目录中创建的文件上调用.createNewFile()。在框架中,对该方法的注释说它没有用。这是一个示例...


 String myPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS) + File.separator + "My Directory";
            final File myDir = new File(myPath);
            try {
                myDir.mkdirs();
            } catch (Exception ex) {
                Toast.makeText(this, "error: " + ex.getMessage(), Toast.LENGTH_LONG).show();
            }

        String fname = "whatever";
        File newFile = new File(myDir, fname);

        Log.i(TAG, "File exists --> " + newFile.exists()) //will be false  
    try {
            if (newFile.createNewFile()) {

                 //continue 

              } else {

                Log.e(TAG, "error creating file");

            }

        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }


0

我创建了一个utils方法来检查SD卡在设备上是否可用,并获取设备上的SD卡路径(如果有)。

您可以将以下两种方法复制到所需的项目类中。就这样。

public String isRemovableSDCardAvailable() {
    final String FLAG = "mnt";
    final String SECONDARY_STORAGE = System.getenv("SECONDARY_STORAGE");
    final String EXTERNAL_STORAGE_DOCOMO = System.getenv("EXTERNAL_STORAGE_DOCOMO");
    final String EXTERNAL_SDCARD_STORAGE = System.getenv("EXTERNAL_SDCARD_STORAGE");
    final String EXTERNAL_SD_STORAGE = System.getenv("EXTERNAL_SD_STORAGE");
    final String EXTERNAL_STORAGE = System.getenv("EXTERNAL_STORAGE");

    Map<Integer, String> listEnvironmentVariableStoreSDCardRootDirectory = new HashMap<Integer, String>();
    listEnvironmentVariableStoreSDCardRootDirectory.put(0, SECONDARY_STORAGE);
    listEnvironmentVariableStoreSDCardRootDirectory.put(1, EXTERNAL_STORAGE_DOCOMO);
    listEnvironmentVariableStoreSDCardRootDirectory.put(2, EXTERNAL_SDCARD_STORAGE);
    listEnvironmentVariableStoreSDCardRootDirectory.put(3, EXTERNAL_SD_STORAGE);
    listEnvironmentVariableStoreSDCardRootDirectory.put(4, EXTERNAL_STORAGE);

    File externalStorageList[] = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        externalStorageList = getContext().getExternalFilesDirs(null);
    }
    String directory = null;
    int size = listEnvironmentVariableStoreSDCardRootDirectory.size();
    for (int i = 0; i < size; i++) {
        if (externalStorageList != null && externalStorageList.length > 1 && externalStorageList[1] != null)
            directory = externalStorageList[1].getAbsolutePath();
        else
            directory = listEnvironmentVariableStoreSDCardRootDirectory.get(i);

        directory = canCreateFile(directory);
        if (directory != null && directory.length() != 0) {
            if (i == size - 1) {
                if (directory.contains(FLAG)) {
                    Log.e(getClass().getSimpleName(), "SD Card's directory: " + directory);
                    return directory;
                } else {
                    return null;
                }
            }
            Log.e(getClass().getSimpleName(), "SD Card's directory: " + directory);
            return directory;
        }
    }
    return null;
}

/**
 * Check if can create file on given directory. Use this enclose with method
 * {@link BeginScreenFragement#isRemovableSDCardAvailable()} to check sd
 * card is available on device or not.
 * 
 * @param directory
 * @return
 */
public String canCreateFile(String directory) {
    final String FILE_DIR = directory + File.separator + "hoang.txt";
    File tempFlie = null;
    try {
        tempFlie = new File(FILE_DIR);
        FileOutputStream fos = new FileOutputStream(tempFlie);
        fos.write(new byte[1024]);
        fos.flush();
        fos.close();
        Log.e(getClass().getSimpleName(), "Can write file on this directory: " + FILE_DIR);
    } catch (Exception e) {
        Log.e(getClass().getSimpleName(), "Write file error: " + e.getMessage());
        return null;
    } finally {
        if (tempFlie != null && tempFlie.exists() && tempFlie.isFile()) {
            // tempFlie.delete();
            tempFlie = null;
        }
    }
    return directory;
}

-1

它适用于所有外部设备,但是请确保仅获取外部设备文件夹名称,然后需要使用File类从给定位置获取文件。

public static List<String> getExternalMounts() {
        final List<String> out = new ArrayList<>();
        String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*";
        String s = "";
        try {
            final Process process = new ProcessBuilder().command("mount")
                    .redirectErrorStream(true).start();
            process.waitFor();
            final InputStream is = process.getInputStream();
            final byte[] buffer = new byte[1024];
            while (is.read(buffer) != -1) {
                s = s + new String(buffer);
            }
            is.close();
        } catch (final Exception e) {
            e.printStackTrace();
        }

        // parse output
        final String[] lines = s.split("\n");
        for (String line : lines) {
            if (!line.toLowerCase(Locale.US).contains("asec")) {
                if (line.matches(reg)) {
                    String[] parts = line.split(" ");
                    for (String part : parts) {
                        if (part.startsWith("/"))
                            if (!part.toLowerCase(Locale.US).contains("vold"))
                                out.add(part);
                    }
                }
            }
        }
        return out;
    }

致电:

List<String> list=getExternalMounts();
        if(list.size()>0)
        {
            String[] arr=list.get(0).split("/");
            int size=0;
            if(arr!=null && arr.length>0) {
                size= arr.length - 1;
            }
            File parentDir=new File("/storage/"+arr[size]);
            if(parentDir.listFiles()!=null){
                File parent[] = parentDir.listFiles();

                for (int i = 0; i < parent.length; i++) {

                    // get file path as parent[i].getAbsolutePath());

                }
            }
        }

访问外部存储

为了在外部存储上读取或写入文件,您的应用必须获得READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE系统权限。例如:

<manifest ...>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    ...
</manifest>

-2

/ sdcard =>内部存储(这是一个符号链接,但应该可以使用)

/ mnt / extSdCard =>外部Sdcard

这是为三星Galaxy S3

您可能可以相信大多数情况都是如此...但是仔细检查!


8
我有几种不同的Android手机,其中大约有一半是三星手机,而且从未见过使用此位置的情况。在S3上可能是正确的,但是说“您可能可以相信大多数情况下都是如此”是完全错误的。
Geobits

错误。/sdcard是一个符号链接到外部对我的索尼2305
jiggunjer

2
我不是说可能不是吗?
robbyoconnor
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.