android获取内置和外置SD卡路径
我的微信公众号

/** * 获取内置SD卡路径 * @return */ public String getInnerSDCardPath() { return Environment.getExternalStorageDirectory().getPath(); } /** * 获取外置SD卡路径 * @return 应该就一条记录或空 */ public List getExtSDCardPath() { List lResult = new ArrayList(); 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]; File file = new File(path); if (file.isDirectory()) { lResult.add(path); } } } isr.close(); } catch (Exception e) { } return lResult; }
StringBuilder log = new StringBuilder();
String inPath = getInnerSDCardPath();
log.append("内置SD卡路径:" + inPath + "\r\n");
List extPaths = getExtSDCardPath();
int num=extPaths.size();
for (int i=0;i<num;i++) {
String path=extPaths.get(i).toString();
log.append("外置SD卡路径:" + path + "\r\n");
}
System.out.println(log.toString());
// String rasterPath = inPath+ "/raster/r.tif";
String rasterPath = extPaths.get(0).toString()+ "/raster1/r.tif";
FileRasterSource rasterSource=null;
String TAG="闫磊";
try {
rasterSource = new FileRasterSource(rasterPath);
} catch (IllegalArgumentException ie) {
Log.d(TAG, "null or empty path");
} catch (FileNotFoundException fe) {
Log.d(TAG, "raster file:"+rasterPath+" doesn't exist");
} catch (RuntimeException re) {
Log.d(TAG, "raster file can't be opened");
}
Log.d(TAG,"加载影像:"+ rasterPath);
RasterLayer rasterLayer = new RasterLayer(rasterSource);
mMapView.addLayer(rasterLayer);
浙公网安备 33010602011771号