2013-07-05 4 views
2

개발중인 응용 프로그램의 경우 SD 카드의 텍스트 파일에 텍스트를 쓰려고합니다. 나는 아래 코드를 사용하여이를 시도했다. 이 오류는 있지만 왜 볼 수 없습니다. 내가 말할 수있는 한, 나는 웹상의 모든 예들을 완벽하게 따라 갔다. logcat에서 첫 번째 로그는 보이지만 두 번째 로그는 보이지 않으므로 문제는 파일 생성에 있습니다. 너희들은 무슨 일이 일어날 지 생각하고 있니?파일을 저장할 때 해당 파일이나 디렉토리가 없습니다.

public void saveDataToFile(String data, String fileName) { 

    Log.d("Checks", "Trying to save data"); 

    try { 
     // Set up the file directory 
     String filePath = Environment.getExternalStorageDirectory().toString() + "/Data Folder"; 
     File fileDirectory = new File(filePath); 
     fileDirectory.mkdirs(); 
     Log.d("Checks", "Directory created"); 

     // Set up the file itself 
     File textFile = new File(fileDirectory, fileName); 
     textFile.createNewFile(); 
     Log.d("Checks", "File created"); 

     // Write to the file 
     FileOutputStream fileOutputStream = new FileOutputStream(textFile); 
     OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream); 
     outputStreamWriter.append(data); 
     outputStreamWriter.close(); 
     fileOutputStream.close(); 

     Toast.makeText(mContext, "Done writing to SD card", Toast.LENGTH_SHORT).show(); 
    } catch (Exception e) { 
     Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show(); 
    } 

} 

편집 :

내가 매니페스트에 대한 권리 권한을 추가 잊고 있었던 밝혀졌습니다. 이제 작동합니다!

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

에뮬레이터로 테스트 중이십니까? – iTurki

+1

외부 SD 카드에 액세스하여 쓸 수있는 적절한 사용 권한을 선언 했습니까? –

+0

음, 나는 어리석은 느낌! 매니페스트에서 권한을 설정하는 것을 잊었습니다. :). – Zero

답변

2

외부 SD 카드에 액세스하고 쓰기 위해 적절한 사용 권한을 선언 했습니까?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

올바른 것으로 표시하시기 바랍니다. –

관련 문제