如何正确获得手机的内置,外置SD卡路径?

Kav3000 2016-01-13 02:40:17
Environment.getExternalStorageDirectory().getAbsolutePath()

在华为荣耀手机上获得的路径是: /storage/emulated/0

实际SD卡路径是:
内置:storage/sdcard0
外置:storage/sdcard1

求解决方案,感觉这个问题应该不难,但是搜索不到合适的答案
...全文
32833 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_35810532 2016-08-07
  • 打赏
  • 举报
回复
SD卡/Android/data/com.tencent.mtt/files/QQBrowser
请叫我活雷锋
danceflash 2016-01-30
  • 打赏
  • 举报
回复
引用 13 楼 kidass_x 的回复:
[quote=引用 12 楼 danceflash 的回复:]。
你好!谢谢你的回复! 我尝试将上述中的两个系统文件的内容导出并查看,内容如下: sony: /system/etc/vold.fstab # Copyright (c) 2011, Code Aurora Forum. All rights reserved. # Copyright (c) 2012-2013 Sony Mobile Communications AB. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above # copyright notice, this list of conditions and the following # disclaimer in the documentation and/or other materials provided # with the distribution. # * Neither the name of Code Aurora Forum, Inc. nor the names of its # contributors may be used to endorse or promote products derived # from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # NOTE: This file has been modified by Sony Mobile Communications AB. # Modifications are licensed under the licence above. dev_mount ext_card /storage/removable/sdcard1 auto /devices/platform/msm_sdcc.3/mmc_host dev_mount usbdisk /storage/removable/usbdisk auto /devices/platform/msm_hsusb_host /proc/mounts rootfs / rootfs ro,relatime 0 0 tmpfs /dev tmpfs rw,nosuid,relatime,mode=755 0 0 devpts /dev/pts devpts rw,relatime,mode=600 0 0 proc /proc proc rw,relatime 0 0 sysfs /sys sysfs rw,relatime 0 0 /sys/kernel/debug /sys/kernel/debug debugfs rw,relatime 0 0 none /acct cgroup rw,relatime,cpuacct 0 0 tmpfs /mnt/secure tmpfs rw,relatime,mode=700 0 0 tmpfs /mnt/asec tmpfs rw,relatime,mode=755,gid=1000 0 0 tmpfs /mnt/obb tmpfs rw,relatime,mode=755,gid=1000 0 0 none /dev/cpuctl cgroup rw,relatime,cpu 0 0 /dev/block/platform/msm_sdcc.1/by-name/system /system ext4 ro,relatime,data=ordered 0 0 /dev/block/platform/msm_sdcc.1/by-name/userdata /data ext4 rw,nosuid,nodev,relatime,discard,noauto_da_alloc,data=ordered 0 0 /dev/block/platform/msm_sdcc.1/by-name/apps_log /mnt/idd ext4 rw,nosuid,nodev,noexec,noatime,discard,nobarrier,data=ordered 0 0 /dev/block/platform/msm_sdcc.1/by-name/cache /cache ext4 rw,nosuid,nodev,noatime,discard,data=ordered 0 0 /dev/block/platform/msm_sdcc.1/by-name/LTALabel /lta-label ext4 ro,nosuid,nodev,noexec,noatime,nobarrier,data=ordered 0 0 tmpfs /mnt/qcks tmpfs rw,relatime,mode=770,uid=1000,gid=1000 0 0 /dev/fuse /mnt/shell/emulated fuse rw,nosuid,nodev,noexec,relatime,user_id=1023,group_id=1023,default_permissions,allow_other,allow_utime_grp 0 0 /dev/fuse /storage/emulated/legacy fuse rw,nosuid,nodev,noexec,relatime,user_id=1023,group_id=1023,default_permissions,allow_other,allow_utime_grp 0 0 可以注意到,这里并没有真正的sdcard路径,这是为什么呢? 注意:因为华为没有权限,所以找的索尼设备,平板spg321-CB5AQ1KVK [/quote] 这个平板是否安装了外置sd卡?如果没有挂载sd卡,路径是不会显示出来的。另外这两个文件应该是不需要权限的,你只要能够通过 adb shell 访问到设备(打开develop option,并打开USB debugging),应该就可以读取。 你再试一下这个代码,这个是我们精简过的,因为我们觉得原来的那个代码条件太多,可能找不到:

public static List<String> getStorageList() throws IOException {
	File file = new File("/proc/mounts");
	if (file.canRead()) {
		BufferedReader reader = null;
		List<String> result = new ArrayList<String>();
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
		String lines;
		while ((lines = reader.readLine()) != null) {
			String[] parts = lines.split("\\s+");
			if (parts.length >= 2) {
				if (parts[0].contains("vold")) {
					result.add(parts[1]);
				}
			}
		}
		return result;
	}
	return null;
}
如果这个代码还找不到的话,那我就真帮不了你了,因为正常来说linux的挂载情况都会记录到 /proc/mounts 里面,如果你手里的设备在SD卡已经挂载的情况下,仍然无法在 /proc/mounts 里面找到对应路径,那只能说你手里的设备所使用的linux与众不同,可能是被深度定制化过的,包括你说华为那个手机没有权限读取这个文件也可能是这个问题。这种情况下可能你需要通过写程序的方式,将你程序能访问到的文件系统导出来去找到这台设备对应的挂载信息文件了。
Kav3000 2016-01-28
  • 打赏
  • 举报
回复
引用 12 楼 danceflash 的回复:
你好!谢谢你的回复! 我尝试将上述中的两个系统文件的内容导出并查看,内容如下: sony: /system/etc/vold.fstab # Copyright (c) 2011, Code Aurora Forum. All rights reserved. # Copyright (c) 2012-2013 Sony Mobile Communications AB. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above # copyright notice, this list of conditions and the following # disclaimer in the documentation and/or other materials provided # with the distribution. # * Neither the name of Code Aurora Forum, Inc. nor the names of its # contributors may be used to endorse or promote products derived # from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # NOTE: This file has been modified by Sony Mobile Communications AB. # Modifications are licensed under the licence above. dev_mount ext_card /storage/removable/sdcard1 auto /devices/platform/msm_sdcc.3/mmc_host dev_mount usbdisk /storage/removable/usbdisk auto /devices/platform/msm_hsusb_host /proc/mounts rootfs / rootfs ro,relatime 0 0 tmpfs /dev tmpfs rw,nosuid,relatime,mode=755 0 0 devpts /dev/pts devpts rw,relatime,mode=600 0 0 proc /proc proc rw,relatime 0 0 sysfs /sys sysfs rw,relatime 0 0 /sys/kernel/debug /sys/kernel/debug debugfs rw,relatime 0 0 none /acct cgroup rw,relatime,cpuacct 0 0 tmpfs /mnt/secure tmpfs rw,relatime,mode=700 0 0 tmpfs /mnt/asec tmpfs rw,relatime,mode=755,gid=1000 0 0 tmpfs /mnt/obb tmpfs rw,relatime,mode=755,gid=1000 0 0 none /dev/cpuctl cgroup rw,relatime,cpu 0 0 /dev/block/platform/msm_sdcc.1/by-name/system /system ext4 ro,relatime,data=ordered 0 0 /dev/block/platform/msm_sdcc.1/by-name/userdata /data ext4 rw,nosuid,nodev,relatime,discard,noauto_da_alloc,data=ordered 0 0 /dev/block/platform/msm_sdcc.1/by-name/apps_log /mnt/idd ext4 rw,nosuid,nodev,noexec,noatime,discard,nobarrier,data=ordered 0 0 /dev/block/platform/msm_sdcc.1/by-name/cache /cache ext4 rw,nosuid,nodev,noatime,discard,data=ordered 0 0 /dev/block/platform/msm_sdcc.1/by-name/LTALabel /lta-label ext4 ro,nosuid,nodev,noexec,noatime,nobarrier,data=ordered 0 0 tmpfs /mnt/qcks tmpfs rw,relatime,mode=770,uid=1000,gid=1000 0 0 /dev/fuse /mnt/shell/emulated fuse rw,nosuid,nodev,noexec,relatime,user_id=1023,group_id=1023,default_permissions,allow_other,allow_utime_grp 0 0 /dev/fuse /storage/emulated/legacy fuse rw,nosuid,nodev,noexec,relatime,user_id=1023,group_id=1023,default_permissions,allow_other,allow_utime_grp 0 0 可以注意到,这里并没有真正的sdcard路径,这是为什么呢? 注意:因为华为没有权限,所以找的索尼设备,平板spg321-CB5AQ1KVK
danceflash 2016-01-28
  • 打赏
  • 举报
回复
引用 10 楼 kidass_x 的回复:
[quote=引用 9 楼 danceflash 的回复:] Environment.getExternalStorageDirectory()是Android 2.x时代的产物,那时Android主流设备只有很小的内置存储器,然后都会外置一张sd卡,那时这个方法返回的就是外置sd卡的根路径。 但随着Android进入4.x时代,大部分Android设备都已经内置一个较大存储卡,甚至不提供外置存储卡插槽了,此时Environment.getExternalStorageDirectory()返回的路径其实是由内置存储卡虚拟出来的一个目录,用户在这一目录下有全部权限。 按理说Android应该继续提供其他的方法来返回其他的外置存储卡的路径,但这一次Android却没有,因为Android系统开发者们发现之前直接允许程序任意访问sd卡的做法导致sd卡上垃圾文件的不可管理性,尤其在程序被卸载后,由于不知道那些文件是由这一程序创建的,所以无法关联删除,因此Android 4.0并不给程序开发者提供外置存储卡的路径,而是提供了诸如getExternalFilesDirs()和getExternalCacheDirs()这样的方法,直接给开发者提供一个限制内的存储文件的空间,这个空间是与程序关联的,因此在卸载程序时这两个目录下的文件也会被关联删除。 闲话到此,主要是为了说明Android 4.0之后,系统不希望开发者直接访问sd卡,所以也没有提供查询多sd卡路径的方法(在2.x和3.x时代末期我们都以为4.0会提供这样的查询接口)。 但是,Android系统不提供并不意味着就没有办法了,Android说到底还是Linux系统,因此外置sd卡必然是按照Linux的挂载方式挂载到系统中的。因此目前比较主流的方法就是绕过Android直接去读取Linux底层的挂载文件来找到系统中挂载的外置存储器。 参考代码:(这个代码是从网上找的,跟我之前写的思路差不多,参考这个改改吧,其实不用这么复杂,返回一个Set或者List把所有路径都返回就好了)

    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;
    }
这个为什么在华为手机上只能获得第一个SDCARD呢?实际上,这个手机有外置卡的?[/quote] 你是说这个代码在华为的手机上无法找到SD卡的路径是吧,有可能华为的手机修改了Linux系统关于mount有关的存放方式或者标志。很可惜因为我在海外,所以测试设备中不包含华为设备,所以无法帮你试验,你可以尝试阅读一下代码涉及的两个系统文件,看看在里面是否包含外置SD卡的挂载路径(从你的问题可知你已经知道实际挂载路径了),如果有的话检查一下前后的挂载信息,对比代码看看为什么没有找到。如果这两个系统文件中都没有出现外置SD卡的挂载路径,那说明华为的Linux系统将外置SD卡的挂载路径存到别处去了。
Kav3000 2016-01-27
  • 打赏
  • 举报
回复
引用 9 楼 danceflash 的回复:
Environment.getExternalStorageDirectory()是Android 2.x时代的产物,那时Android主流设备只有很小的内置存储器,然后都会外置一张sd卡,那时这个方法返回的就是外置sd卡的根路径。 但随着Android进入4.x时代,大部分Android设备都已经内置一个较大存储卡,甚至不提供外置存储卡插槽了,此时Environment.getExternalStorageDirectory()返回的路径其实是由内置存储卡虚拟出来的一个目录,用户在这一目录下有全部权限。 按理说Android应该继续提供其他的方法来返回其他的外置存储卡的路径,但这一次Android却没有,因为Android系统开发者们发现之前直接允许程序任意访问sd卡的做法导致sd卡上垃圾文件的不可管理性,尤其在程序被卸载后,由于不知道那些文件是由这一程序创建的,所以无法关联删除,因此Android 4.0并不给程序开发者提供外置存储卡的路径,而是提供了诸如getExternalFilesDirs()和getExternalCacheDirs()这样的方法,直接给开发者提供一个限制内的存储文件的空间,这个空间是与程序关联的,因此在卸载程序时这两个目录下的文件也会被关联删除。 闲话到此,主要是为了说明Android 4.0之后,系统不希望开发者直接访问sd卡,所以也没有提供查询多sd卡路径的方法(在2.x和3.x时代末期我们都以为4.0会提供这样的查询接口)。 但是,Android系统不提供并不意味着就没有办法了,Android说到底还是Linux系统,因此外置sd卡必然是按照Linux的挂载方式挂载到系统中的。因此目前比较主流的方法就是绕过Android直接去读取Linux底层的挂载文件来找到系统中挂载的外置存储器。 参考代码:(这个代码是从网上找的,跟我之前写的思路差不多,参考这个改改吧,其实不用这么复杂,返回一个Set或者List把所有路径都返回就好了)

    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;
    }
这个为什么在华为手机上只能获得第一个SDCARD呢?实际上,这个手机有外置卡的?
danceflash 2016-01-20
  • 打赏
  • 举报
回复
Environment.getExternalStorageDirectory()是Android 2.x时代的产物,那时Android主流设备只有很小的内置存储器,然后都会外置一张sd卡,那时这个方法返回的就是外置sd卡的根路径。 但随着Android进入4.x时代,大部分Android设备都已经内置一个较大存储卡,甚至不提供外置存储卡插槽了,此时Environment.getExternalStorageDirectory()返回的路径其实是由内置存储卡虚拟出来的一个目录,用户在这一目录下有全部权限。 按理说Android应该继续提供其他的方法来返回其他的外置存储卡的路径,但这一次Android却没有,因为Android系统开发者们发现之前直接允许程序任意访问sd卡的做法导致sd卡上垃圾文件的不可管理性,尤其在程序被卸载后,由于不知道那些文件是由这一程序创建的,所以无法关联删除,因此Android 4.0并不给程序开发者提供外置存储卡的路径,而是提供了诸如getExternalFilesDirs()和getExternalCacheDirs()这样的方法,直接给开发者提供一个限制内的存储文件的空间,这个空间是与程序关联的,因此在卸载程序时这两个目录下的文件也会被关联删除。 闲话到此,主要是为了说明Android 4.0之后,系统不希望开发者直接访问sd卡,所以也没有提供查询多sd卡路径的方法(在2.x和3.x时代末期我们都以为4.0会提供这样的查询接口)。 但是,Android系统不提供并不意味着就没有办法了,Android说到底还是Linux系统,因此外置sd卡必然是按照Linux的挂载方式挂载到系统中的。因此目前比较主流的方法就是绕过Android直接去读取Linux底层的挂载文件来找到系统中挂载的外置存储器。 参考代码:(这个代码是从网上找的,跟我之前写的思路差不多,参考这个改改吧,其实不用这么复杂,返回一个Set或者List把所有路径都返回就好了)

    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;
    }
Kav3000 2016-01-19
  • 打赏
  • 举报
回复


这个问题应该不难吧,为什么没人回答呢?
yunmenggyy 2016-01-19
  • 打赏
  • 举报
回复
引用 6 楼 stven_king 的回复:
[quote=引用 楼主 kidass_x 的回复:] Environment.getExternalStorageDirectory().getAbsolutePath() 在华为荣耀手机上获得的路径是: /storage/emulated/0 实际SD卡路径是: 内置:storage/sdcard0 外置:storage/sdcard1 求解决方案,感觉这个问题应该不难,但是搜索不到合适的答案
内置:storage/sdcard0 外置:storage/sdcard1 这两其实上是快捷方式,实际上是 /storage/emulated/0 /storage/emulated/1 可以自己用手机测试下。。。。[/quote] 是的,有的甚至在extsd下。
bug员 2016-01-19
  • 打赏
  • 举报
回复
引用 5 楼 stven_king 的回复:
引用 3 楼 oAiTan 的回复:
String sdcard = Environment.getExternalStorageDirectory().toString();
这个使用的时候应该判null,在有些手机上是为空的。
额 能不能具体出机型,华为 htc 小米 亲儿子,三星,中兴 测了 都没有问题。
静默加载 应用层 2016-01-19
  • 打赏
  • 举报
回复
引用 楼主 kidass_x 的回复:
Environment.getExternalStorageDirectory().getAbsolutePath() 在华为荣耀手机上获得的路径是: /storage/emulated/0 实际SD卡路径是: 内置:storage/sdcard0 外置:storage/sdcard1 求解决方案,感觉这个问题应该不难,但是搜索不到合适的答案
内置:storage/sdcard0 外置:storage/sdcard1 这两其实上是快捷方式,实际上是 /storage/emulated/0 /storage/emulated/1 可以自己用手机测试下。。。。
静默加载 应用层 2016-01-19
  • 打赏
  • 举报
回复
引用 3 楼 oAiTan 的回复:
String sdcard = Environment.getExternalStorageDirectory().toString();
这个使用的时候应该判null,在有些手机上是为空的。
bug员 2016-01-19
  • 打赏
  • 举报
回复
例如内存卡下面DCIM这个文件夹,sdcard+"/DCIM"
bug员 2016-01-19
  • 打赏
  • 举报
回复
String sdcard = Environment.getExternalStorageDirectory().toString();
Kaede31416 2016-01-19
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/390431182?page=1

80,361

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧