Android获取外置卡和内置卡信息的方法

如果手机支持TF卡扩展,并且本身没有大容量存储,比如说HTC老款手机 G3之类的,那么获取到的是TF卡的路径。 Android获取外部SDcard的官方调用是
Environment.getExternalStorageDirectory()。

如果手机本身有大容量存储,不论手机是否支持TF卡扩展,比如现在HTC系列高端手机One X,或者920 butterfly系列,那么获取到的是大容量存储的路径。也就是说,如果手机本身具有大容量存储,又支持TF卡扩展,通过这条命令是无法获取到TF卡信息的。

如下就说说怎样同时获取TF卡信息

工具类

public class SDcardInfo {
    public final String HEAD = "dev_mount";
    public final String LABEL = "<label>";
    public final String MOUNT_POINT = "<mount_point>";
    public final String PATH = "<part>";
    public final String SYSFS_PATH = "<sysfs_path1...>";

    private final int NLABEL = 1; // Label for the volume
    private final int NPATH = 2; // The volume path
    private final int NMOUNT_POINT = 3; // Where the volume will be mounted
    private final int NSYSFS_PATH = 4;

    private final int DEV_INTERNAL = 0;
    private final int DEV_EXTERNAL = 1;

    private ArrayList<String> cache = new ArrayList<String>();

    private static SDcardInfo dev;

    private final File VOLD_FSTAB = new File(Environment.getRootDirectory()
            .getAbsoluteFile() + File.separator + "etc" + File.separator
            + "vold.fstab");

    public static SDcardInfo getInstance() {
        if (null == dev)
            dev = new SDcardInfo();
        return dev;
    }

    private DevInfo getInfo(final int device) {
        DevInfo info = new DevInfo();
        try {
            initVoldFstabToCache();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (device >= cache.size()) {
            return null;
        }
        String[] sinfo = cache.get(device).split(" ");

        info.setLabel(sinfo[NLABEL]);
        info.setMountPoint(sinfo[NMOUNT_POINT]);
        info.setPath(sinfo[NPATH]);
        info.setSysfsPath(sinfo[NSYSFS_PATH]);
        return info;
    }

    /**
     * init the words into the cache array
     *
     */
    private void initVoldFstabToCache() throws IOException {
        cache.clear();
        BufferedReader br = new BufferedReader(new FileReader(VOLD_FSTAB));
        String tmp = null;
        while ((tmp = br.readLine()) != null) {
            // the words startsWith "dev_mount" are the SD info
            if (tmp.startsWith(HEAD)) {
                cache.add(tmp);
            }
        }
        br.close();
        cache.trimToSize();
    }

    public class DevInfo {
        private String label, mountPoint, path, sysfsPath;

        /**
         * return the label name of the SD card
         */
        public String getLabel() {
            return label;
        }

        private void setLabel(String label) {
            this.label = label;
        }

        /**
         * the mount point of the SD card
         */
        public String getMountPoint() {
            return mountPoint;
        }

        private void setMountPoint(String mountPoint) {
            this.mountPoint = mountPoint;
        }

        /**
         * SD mount path
         */
        public String getPath() {
            return path;
        }

        private void setPath(String path) {
            this.path = path;
        }

        /**
         * "unknow"
         */
        public String getSysfsPath() {
            return sysfsPath;
        }

        private void setSysfsPath(String sysfs_path) {
            this.sysfsPath = sysfsPath;
        }
    }

    public DevInfo getInternalInfo() {
        return getInfo(DEV_INTERNAL);
    }

    public DevInfo getExternalInfo() {
        return getInfo(DEV_EXTERNAL);
    }

}


工具类的使用

        SDcardInfo dev1 = new SDcardInfo();
        SDcardInfo dev2 = new SDcardInfo();
        DevInfo internalInfo = dev1.getInternalInfo();
        DevInfo externalInfo = dev2.getExternalInfo();

    String internalPath ;
    String externalPath;
        if (internalInfo != null) {
             internalPath = internalInfo.getPath(); // Internal path
        }
        if (externalInfo != null) {
            externalPath = externalInfo.getPath();// External path
        }

如果internalPath和externalPath 都为null ,则说明没有sdcard,如果externalPath 为 null 则说明没有外置卡。但当没有内置卡时插入TF卡,internalPath 不为 null,而externalPath为null ,说明此时的TF卡以内置卡的形式mount在机器内。



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值