现在的位置: 首页 > 综合 > 正文

android 获取外置sd卡根目录

2014年02月22日 ⁄ 综合 ⁄ 共 1039字 ⁄ 字号 评论关闭

Android获取外部SDcard的官方调用是
Environment.getExternalStorageDirectory()

但是根据不同机型,是有不同的结果的。

通过 android 的api可以获取系统内置的sdcard存贮目录,但是有些机器本身具有内置的sdcard存贮卡又可以外置SD卡,这样通过系统api获取的路径就只能是内置存贮卡目录,有些手机可能有两个存贮目录mnt/sdcard/     mnt/extsdcard或者storage/sdcard0    storage/extsdcard

so:

通过如下方式获得

try {
			Runtime runtime = Runtime.getRuntime();
			Process proc = runtime.exec("mount");
			InputStream is = proc.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			String line;
			String mount = new String();
			BufferedReader br = new BufferedReader(isr);
			while ((line = br.readLine()) != null) {
				if (line.contains("secure")) continue;
				if (line.contains("asec")) continue;
				
				if (line.contains("fat")) {
					String columns[] = line.split(" ");
					if (columns != null && columns.length > 1) {
						mount = mount.concat("*" + columns[1] + "\n");
					}
				} else if (line.contains("fuse")) {
					String columns[] = line.split(" ");
					if (columns != null && columns.length > 1) {
						mount = mount.concat(columns[1] + "\n");
					}
				}
			}
			txtView.setText(mount);
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

抱歉!评论已关闭.