2012-10-18 1 views
1

다음 코드를 사용하여 Sd 카드가 있는지 확인하고 Writable.But 에뮬레이터의 Sd 카드 컨텍스트에서 해당 코드를 사용할 때 Sd 카드가 에뮬레이터에 없지만 실제로는 탐색기를 사용하여 해당 Emulator.This의 SD 카드는 SD 카드의 내용을 보여주는 파일은 코드입니다 :SD 카드가 존재하고 쓰기 가능 여부를 효율적으로 확인하는 방법은 무엇입니까?

static public boolean hasStorage(boolean requireWriteAccess) { 
    //TODO: After fix the bug, add "if (VERBOSE)" before logging errors. 
    String state = Environment.getExternalStorageState(); 
    System.out.println("storage state is " + state); 

    if (Environment.MEDIA_MOUNTED.equals(state)) { 
     if (requireWriteAccess) { 
      boolean writable = checkFsWritable(); 
      System.out.println("storage writable is " + writable); 
      return writable; 
     } else { 
      return true; 
     } 
    } else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
     return true; 
    } 
    return false; 
} 

이 코드는 SD 카드는 SD 카드가 장착되지 않는다는 것을 보여주고있다 그러나 파일 탐색기는 다른 picture.Please 도움말을 보여주고있다 나. 진심으로 감사합니다.

+0

누군가를 도우십시오. – user1726619

+0

코드를 읽습니다. 2 분 안에 답을 기다리지 마라. –

+0

나는 기다릴 수있다. – user1726619

답변

2

이 작품은 둘 다 탑재 된 것을 확인하고 읽을 수 있는지 여부를 확인합니다.

private boolean isExternalStorageAvailable() { 

     String state = Environment.getExternalStorageState(); 
     boolean mExternalStorageAvailable = false; 
     boolean mExternalStorageWriteable = false; 

     if (Environment.MEDIA_MOUNTED.equals(state)) { 
      // We can read and write the media 
      mExternalStorageAvailable = mExternalStorageWriteable = true; 
     } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
      // We can only read the media 
      mExternalStorageAvailable = true; 
      mExternalStorageWriteable = false; 
     } else { 
      // Something else is wrong. It may be one of many other states, but 
      // all we need 
      // to know is we can neither read nor write 
      mExternalStorageAvailable = mExternalStorageWriteable = false; 
     } 

     if (mExternalStorageAvailable == true 
       && mExternalStorageWriteable == true) { 
      return true; 
     } else { 
      return false; 
     } 
    } 
+0

Sd 카드가 마운트되어 있는지 확인하는 것과 관련하여 코드에 문제가 있습니까? – user1726619

+0

아니,하지만 위의 코드를 테스트 한 두 에뮬레이터 및 장치, 잘 작동합니다. –

+0

또한 sd 카드가 없지만 실제로는 같은 결과를 보여줍니다. – user1726619