2016-08-28 77 views
0

目前我有這個代碼,我從here複製粘貼到我的源代碼。但由於某種原因我不知道,OutputStreamWriter繼續給IOException。而且我對這是什麼原因以及如何解決這個問題毫無頭緒。請幫忙。以下是我的代碼。OutputStreamWriter IOException發生

public void generateNoteOnSD(){// create a File object for the parent directory 
    try { 
     File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 
     File myFile = new File(path, "mytextfile.txt"); 
     FileOutputStream fOut = new FileOutputStream(myFile,true); 
     OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); 
     myOutWriter.append("the text I want added to the file"); 

     Log.d("Save File Test : ","Success"); 

     myOutWriter.close(); 
     fOut.close(); 
    } 
    catch (IOException e) { 
     //do something if an IOException occurs. 
     Log.d("Save File Test : ","Failed"); 
    } 
} 

我已經列入

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
在清單

,並運行代碼AsyncTask - onPostExecute和AVD棒棒糖版本。

我的堆棧跟蹤:

08-28 11:22:52.788 30478-30478/com.example.azrie.dummyvoice E/YOUR_APP_LOG_TAG: I got an error 
java.io.FileNotFoundException: /storage/emulated/0/Download/mytextfile.txt: open failed: EACCES (Permission denied) 
at libcore.io.IoBridge.open(IoBridge.java:452) 
at java.io.FileOutputStream.<init>(FileOutputStream.java:87) 
at com.example.azrie.dummyvoice.ReadHTML.generateNoteOnSD(ReadHTML.java:126) 
at com.example.azrie.dummyvoice.ReadHTML.onPostExecute(ReadHTML.java:78) 
at com.example.azrie.dummyvoice.ReadHTML.onPostExecute(ReadHTML.java:37) 
at android.os.AsyncTask.finish(AsyncTask.java:651) 
at android.os.AsyncTask.-wrap1(AsyncTask.java) 
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied) 
at libcore.io.Posix.open(Native Method) 
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186) 
at libcore.io.IoBridge.open(IoBridge.java:438) 
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)  
at com.example.azrie.dummyvoice.ReadHTML.generateNoteOnSD(ReadHTML.java:126)  
at com.example.azrie.dummyvoice.ReadHTML.onPostExecute(ReadHTML.java:78)  
at com.example.azrie.dummyvoice.ReadHTML.onPostExecute(ReadHTML.java:37)  
at android.os.AsyncTask.finish(AsyncTask.java:651)  
at android.os.AsyncTask.-wrap1(AsyncTask.java)  
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:148)  
at android.app.ActivityThread.main(ActivityThread.java:5417)  
at java.lang.reflect.Method.invoke(Native Method)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
+0

也許如果您提供IOException的堆棧跟蹤,我們可以幫助 –

+0

你好。我添加了我的堆棧跟蹤。謝謝。 –

+1

這可能有助於http://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android –

回答

-1
private static final String FILE_NAME="score.txt"; 
BufferedOutputStream o; 
       try { 
        o = new BufferedOutputStream(new FileOutputStream(new File(getFilesDir(),FILE_NAME))); 
        String s = //here write what you want to write to the file . its got to be string to work 
        o.write(s.getBytes()); 
        o.close(); 
       }catch (FileNotFoundException e){ 

       }catch (IOException e){ 

       } 

對我來說它每一次,每一次它不是打開新文件的工作。 如果你想看到它在Play商店中的行動serch FightWithMath。您需要完全重新啓動應用程序後首次登錄,並在主屏幕上寫它,並在每次贏得遊戲時運行

相關問題