importandroid.os.Bundle;importandroid.os.Environment;importandroid.support.v7.app.AppCompatActivity;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.widget.TextView;importjava.io.BufferedReader;importjava.io.File;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.util.ArrayList;importjava.util.List;public class MainActivity extendsAppCompatActivity {privateTextView myTextView;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView=(TextView)findViewById(R.id.myTextView);
StringBuilder log= newStringBuilder();
String inPath=getInnerSDCardPath();
String str="内置SD卡路径:" + inPath + "
";
log.append("内置SD卡路径:" + inPath + "
");
List extPaths =getExtSDCardPath();for(String path : extPaths) {
str=str+"外置SD卡路径:" + path + "
";
log.append("外置SD卡路径:" + path + "
");
}
myTextView.setText(str);
System.out.println(log.toString());
}/*** 获取内置SD卡路径
*@return
*/
publicString getInnerSDCardPath() {returnEnvironment.getExternalStorageDirectory().getPath();
}/*** 获取外置SD卡路径
*@return应该就一条记录或空*/
public ListgetExtSDCardPath()
{
List lResult = new ArrayList();try{
Runtime rt=Runtime.getRuntime();
Process proc= rt.exec("mount");
InputStream is=proc.getInputStream();
InputStreamReader isr= newInputStreamReader(is);
BufferedReader br= newBufferedReader(isr);
String line;while ((line = br.readLine()) != null) {if (line.contains("extSdCard"))
{
String [] arr= line.split(" ");
String path= arr[1];
File file= newFile(path);if(file.isDirectory())
{
lResult.add(path);
}
}
}
isr.close();
}catch(Exception e) {
}returnlResult;
}