2014-02-18 2 views
-2

아래는 sd 카드 디렉토리를 검색하기 위해 작성된 코드입니다. 명령 실행을 사용하고/proc/mounts를 읽는 것으로 변경되었습니다. 내 질문은 올바른 코드인지 여부입니다.안드로이드 장치의 SD 카드 디렉토리 받기

Linux OS에 익숙하지 않습니다./proc/mounts 경로가 모든 장치에서 동일합니까? 이 코드는 명령 주입 가능성이 없다고 생각합니다.

// Process process = new ProcessBuilder().command("mount").start(); 
     // process.waitFor(); 

     reader = new BufferedReader(new FileReader("/proc/mounts")); 
     String line; 
     while ((line = reader.readLine()) != null) { 
      // Output the line of output from the mount command 
      logger.debug(" {}", line); 

      if (line.startsWith("/dev/block/vold/")) { 
       String[] tokens = line.split(" "); 
       if (tokens.length >= 3 && (tokens[2].equals("vfat") || tokens[2].equals("exfat"))) { 
        String path = tokens[1]; 
        File file = new File(path); 
        if (file.exists() && file.isDirectory()) { 
         logger.debug("Detected SD card at {}", file.getPath()); 

         if (!file.canWrite()) { 
          logger.warn("The SD card path {} is reporting that it is not writable", file.getPath()); 
         } 

         return path; 
        } 
       } 
      } 
     } 

환호, Saurav 안드로이드에

+0

순전히) (안드로이드 sdcard에 경로 –

+0

Environment.getExternalStorageDirectory을 얻기 위해 할 수있는 y를 읽을 경우

또한, 확인하는 속성 Environment.MEDIA_MOUNTED 등에 대해 Environment.getExternalStorageState()를 사용; –

+0

다른 API는 외부 저장소를 다르게 고려할 것이므로이 API를 사용하는 것이 정확하지 않을 것입니다.이 http://stackoverflow.com/questions/9340332/how-can-i-get-the-list-of를 확인하십시오. -mounted-external-storage-of-android-device/19982338 # 19982338 – saurav

답변

4
+0

지금까지이 API를 사용하는 것이 맞지 않을 것입니다. 다른 제조 방식은 외부 저장소를 다르게 고려할 것이기 때문에 정확하지 않습니다. http://stackoverflow.com/questions를 확인하십시오./9340332/how-can-i-get-the-list-of-mounted-external-storage-of-android-device/19982338 # 19982338 – saurav

+0

실제로 문제를 피하기위한 API입니다. – LuigiPower

+0

http : //stackoverflow.com/questions/11281010/how-can-i-get-external-sd-card-path-for-android-4-0. Envrionment.getExternalStorageDirectory()가 모든 상황에서 작동하지 않습니다 – saurav

2

를 통해 모든 장치의 SD 카드의 디렉토리를 얻을

Environment.getExternalStorageDirectory();이를 얻기 위해 사용하십시오 수 있습니다 S 경로 D 카드. SD 카드가 장착 등 :

+0

다른 API를 사용하면 외부 저장소를 다르게 고려할 것이므로이 API를 사용하는 것이 정확하지 않을 것입니다.이 http://stackoverflow.com/questions/9340332/how-can-i-get-the-list-of를 확인하십시오. 마운트 - 외장형 스토리지 - 안드로이드 장치/19982338 # 19982338 – saurav

+0

정말 그것에 대해 잘 모르겠습니다. 그러나 Google 워드 프로세서는이 방법에 액세스 할 수 있다고 말합니다. http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29 –

+0

여기를 확인하십시오. http://stackoverflow.com/questions/11281010/how-can-i-get-external -sd-card-path-for-android-4-0. Envrionment.getExternalStorageDirectory()가 모든 상황에서 작동하지 않습니다. – saurav

관련 문제