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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM