Android使用Linux mount获取SdCard存储目录 -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

    Android的智能手机各不相同,现在很多Android智能手机都是自带存储,有的还带有扩展内存的Sdcard卡槽,

Android使用Linux mount获取SdCard存储目录

    在Android开发中,很多时候根据Android提供的获取SdCard存储方法获取不到不到存储目录的位置,由于Android的系统是linux系统,所以我想到使用linux命令mouunt来获取linux的挂载目录。

    这样呢,基本上在Android上都可以获取到应用数据存放的目录。

    使用命令获取到的目录我并没有遍历,如果你还是获取不到,可以把mount获去到的所有目录都遍历一次。

<code class="hljs" vbscript="">File  sdcard ;@SuppressLint(SdCardPath)    public File getSdCardFile() {        if (sdcard != null) {            return sdcard;        }        List<string>list = getExtSDCardPath();        boolean isRun = true;        if (list.size() > 0) {            sdcard = new File(list.get(list.size() - 1));            if (sdcard.isDirectory()) {                if (sdcard.getFreeSpace() == 0) {                    isRun = true;                } else {                    isRun = false;                }            }        }        if (isRun) {            sdcard = Environment.getExternalStorageDirectory();            if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {                sdcard = new File(/sdcard/);                log.e(sdcard  spance: + sdcard.getFreeSpace());                if (sdcard.getFreeSpace() == 0) {                    sdcard = new File(/sdcard1/);                    log.e(sdcard1 space: + sdcard.getFreeSpace());                }                if (sdcard.getFreeSpace() == 0) {                    sdcard = new File(/sdcard2/);                    log.e(sdcard2 space: + sdcard.getFreeSpace());                }            }        }        log.e(data: + sdcard.getAbsolutePath());        return sdcard;    }    public List<string>getExtSDCardPath() {        List<string>lResult = new ArrayList<string>();        try {            Runtime rt = Runtime.getRuntime();            Process proc = rt.exec(mount);            InputStream is = proc.getInputStream();            InputStreamReader isr = new InputStreamReader(is);            BufferedReader br = new BufferedReader(isr);            String line;            while ((line = br.readLine()) != null) {                if (line.contains(extSdCard)) {                    String[] arr = line.split( );                    String path = arr[1];                    if (path.lastIndexOf(extSdCard) == path.length() - 9) {                        File file = new File(path);                        if (file.isDirectory()) {                            lResult.add(path);                        }                    }                } else if (line.contains(/sdcard)) {                    String[] arr = line.split( );                    String path = arr[1];                    if (path.lastIndexOf(/sdcard) == path.length() - 6) {                        File file = new File(path);                        if (file.isDirectory()) {                            lResult.add(path);                        }                    } else {                        String number = path.substring(path.lastIndexOf(/sdcard) + 7);                        try {                            Integer.parseInt(number);                            File file = new File(path);                            if (file.isDirectory()) {                                lResult.add(path);                            }                        } catch (Exception e) {                        }                    }                }            }            isr.close();        } catch (Exception e) {        }        return lResult;    }</string></string></string></string></code>

    不过,首选获取存储还是先使用Android提供的方法,判断并获取Sdcard目录,

电脑资料

Android使用Linux mount获取SdCard存储目录》(https://www.unjs.com)。

<code class="hljs" mathematica="">//判断sdcard是否存在Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);//如果存在,获取存储File目录Environment.getExternalStorageDirectory();</code>

最新文章