</pre><p>个人总结</p><p><pre name="code" class="java">public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt = (TextView) findViewById(R.id.txt);//显示内置SD路径
TextView txt2 = (TextView) findViewById(R.id.txt2);//显示外置SD路径
StringBuilder log = new StringBuilder();
String inPath = getInnerSDCardPath();
txt.setText("内置SD卡路径:"+inPath);//显示内置SD路径
List<String> extPaths = getExtSDCardPath();
for (String path : extPaths) {
log.append(path);
}
txt2.setText(log.toString());//显示外置SD路径
File f = new File(log.toString() +File.separator+ "mylpic");//获取路径后,在该目录下创建一个文件夹“mylpic”
if (!f.exists()) {
f.mkdirs();
}
}
/**
* 获取内置SD卡路径
*
* @return
*/
public String getInnerSDCardPath() {
return Environment.getExternalStorageDirectory().getPath();
}
/**
* 获取外置SD卡路径
*
* @return 应该就一条记录或空
*/
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;
//不同设备下挂载 的SD卡在不同位置,目前我写了“sd_external“、”sdcard1“这两个。
while ((line = br.readLine()) != null) {
if (line.contains("sd_external")) {
String[] arr = line.split(" ");
String path = arr[1];
File file = new File(path);
if (file.isDirectory()) {
lResult.add(path);
}
} else if (line.contains("sdcard1")) {
String[] arr = line.split(" ");
String path = arr[1];
File file = new File(path);
if (file.isDirectory()) {
lResult.add(path);
}
}
}
isr.close();
} catch (Exception e) {
}
return lResult;
}
}
需要在配置清单中插入以下权限
<!-- SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 向SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />