Monday, February 18, 2019

java - OutputStreamWriter IOException occurs

Answer




Currently I have this code where I copy paste from here into my source code. But due to some reason unknown to me, the OutputStreamWriter keep giving IOException. And I'm clueless on what cause this and how to fix it. Please help. Below is my code.



 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");
}
}



I have already included






in the manifest, and also running the code in AsyncTask - onPostExecute and on AVD with Lollipop version.



My stacktrace:




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.(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.(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) 

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...