2013-03-28 3 views
0

시스템을 부팅 할 때 시작되는 Android의 애플리케이션이 있습니다. 때로는 인터넷 연결이 검색 모드에 있습니다. 그리고 타이머가 연결을 확인한 다음 발견되면 연결합니다. 내가 모드를 준비 중일 때와 마찬가지로 SD 카드를 위해 한 것과 같은 일. 나는 시스템 부팅시 SD 카드에서 텍스트 파일을 읽는 중 문제가 발생하고 응용 프로그램이 SD 카드에서 텍스트를 읽지 못하게 시작합니다. 내가 나중에 수동으로 응용 프로그램을 시작할 때 그것은 작동합니다. 다음은 sd 카드 파일을 읽는 코드입니다.Android SD 카드 읽기

if (isSDCardAvailable()) 
      { 
       setTickerText(); 
      } 

    else if(!isSDCardAvailable()) 
      { 
       //pop up message 
       Toast toast=Toast.makeText(this, "Preparing SD card..", Toast.LENGTH_LONG); 
       toast.show(); 

       //Run the sd card read process after 30 seconds 
       Handler handler = new Handler(); 
       handler.postDelayed(new Runnable() { 
        public void run() 
        { 
         setTickerText(); 
        } 
       }, 30000); 
      } 

public void setTickerText() 
{ 
    File sdcard = Environment.getExternalStorageDirectory(); 

    //Get the text file 
    File file = new File(sdcard,"TickerText.txt"); 

    //Read text from file 
    StringBuilder text = new StringBuilder(); 

    try { 
     BufferedReader br = new BufferedReader(new FileReader(file)); 
     String line; 

     while ((line = br.readLine()) != null) { 
      text.append(line); 
      //text.append('\n'); 
     } 
    } 
    catch (IOException e) { 
     //You'll need to add proper error handling here 
    } 
     } 

답변

0

시스템 부팅 후 앱이 파일 읽기를 시작합니다.

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

    if (Environment.MEDIA_MOUNTED.equals(state)) { 
     if (requireWriteAccess) { 
      boolean writable = checkFsWritable(); 
      Log.v(TAG, "storage writable is " + writable); 
      return writable; 
     } else { 
      return true; 
     } 
    } else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
     return true; 
    } 
    return false; 
} 
+0

이 답변은 잘못된 것입니다 :

그래서 그 전에이 방법을 확인할 수 있습니다. 'Environment.getExternalStorageState()'는 항상 외부 저장소를 반환하지는 않습니다. –