I have a simple app am trying to finish: //listing all of the SD Cards file names in a txt file.
And I've tried so many ways of at least creating the file in the root storage directory but with no luck ( not in the apps private files using "getFilesDir()" ) and one of those ways are shown in the code sample. And yes i used the proper permissions.
Any help would be so much appreciated.
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(path, "Something.txt");
file.mkdirs();
String[] array = file.list();
try {
FileOutputStream fos = new FileOutputStream(file, true);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write(String.valueOf(array));
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
If you are writting to external storage add this permisions to your manifest file:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
...
</manifest>
acording to this [Article.][1]
You need to call osw.close(); for the file to be created.
After digging and playing around a bit,
it turns out that i forgot to to call "fos.close()" and not just "osw.close()"
as well calling "file.createNewFile()" was mandatory , "mkdirs()" isn't working for some reason.
but thanks to everyone, all of your participation and effort were appreciated.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.