Android在mtk平台上获取手机内置存储卡路径

首先,通过反射机制获取swap开关的值:

 /**
     * 通过反射机制获取swap开关的值
     *
     * @return
     */
    public static String getSwap() {
        Object result = null;
        try {
            Class cl = Class.forName("android.os.SystemProperties");
            Object invoker = cl.newInstance();
            Method m = cl.getMethod("get", new Class[] { String.class, String.class });
            result = m.invoke(invoker, new Object[] { "ro.mtk_2sdcard_swap", "unknown" });
            return (String) result;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return (String) result;
    }

然后获取手机内置存储卡路径:

    /**
     * 获取手机内置存储卡路径
     * @return
     */
    public static String getInnerSDPath(){
        List<String> list = SDCardScanner.getExtSDCardPaths();
        String innerSDPath = "/storage/sdcard0";

        if (list.size() == 0) {// 内置内存、外置内存不可用
            innerSDPath = "/storage/sdcard0";

        } else if (list.size() == 1) {// 没有外置内存或者外置内存不可用,内置内存可用
            innerSDPath = "/storage/sdcard0";//list.get(0);

        } else if (list.size() == 2) {// 内外置都可用

            if ("1".equals(getSwap())) {// 开关打开,表示可以切换
                innerSDPath = "/storage/sdcard1";//list.get(1);
            }
            else if ("0".equals(getSwap())) {// 开关关闭,表示不可以切换

                innerSDPath = "/storage/sdcard0";//list.get(0);
            }
        }

        return innerSDPath;
    }

其中SDCardScanner类如下定义:


import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.os.Environment;

/**
 * 手机存储卡浏览类
 * @author weizhi
 * @version 1.0
 */
public class SDCardScanner {
    /*
     * avoid initializations of tool classes
     */
    private SDCardScanner() {
    }

    /**
     * @Title: getExtSDCardPaths
     * @Description: to obtain storage paths, the first path is theoretically
     *               the returned value of
     *               Environment.getExternalStorageDirectory(), namely the
     *               primary external storage. It can be the storage of internal
     *               device, or that of external sdcard. If paths.size() >1,
     *               basically, the current device contains two type of storage:
     *               one is the storage of the device itself, one is that of
     *               external sdcard. Additionally, the paths is directory.
     * @return List<String>
     * @throws IOException
     */
    public static List<String> getExtSDCardPaths() {
        List<String> paths = new ArrayList<String>();
        String extFileStatus = Environment.getExternalStorageState();
        File extFile = Environment.getExternalStorageDirectory();
        if (extFileStatus.equals(Environment.MEDIA_MOUNTED)
                && extFile.exists()
                && extFile.isDirectory()
                && extFile.canWrite()) {
            paths.add(extFile.getAbsolutePath());
        }
        try {
            // obtain executed result of command line code of 'mount', to judge
            // whether tfCard exists by the result
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec("mount");// 执行指定字符串命令
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            int mountPathIndex = 1;
            while ((line = br.readLine()) != null) {
                // format of sdcard file system: vfat/fuse
                if ((!line.contains("fat") && !line.contains("fuse") && !line
                        .contains("storage"))
                        || line.contains("secure")
                        || line.contains("asec")
                        || line.contains("firmware")
                        || line.contains("shell")
                        || line.contains("obb")
                        || line.contains("legacy") || line.contains("data")) {
                    continue;
                }
                String[] parts = line.split(" ");
                int length = parts.length;
                if (mountPathIndex >= length) {
                    continue;
                }
                String mountPath = parts[mountPathIndex];
                if (!mountPath.contains("/") || mountPath.contains("data")
                        || mountPath.contains("Data")) {
                    continue;
                }
                File mountRoot = new File(mountPath);
                if (!mountRoot.exists() || !mountRoot.isDirectory()
                        || !mountRoot.canWrite()) {
                    continue;
                }
                boolean equalsToPrimarySD = mountPath.equals(extFile
                        .getAbsolutePath());
                if (equalsToPrimarySD) {
                    continue;
                }
                paths.add(mountPath);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return paths;
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值