Android Write To File Not Working
I am a beginner when it comes to Android. I encountered a problem, regarding writing to a file. I want to save to a file the input I get in a form. However, the piece of code that
Solution 1:
Use following path for file. It will write file to your root folder of storage.
StringextStorageDirectory= Environment.getExternalStorageDirectory().toString();
File file= newFile(extStorageDirectory, "config.txt");
writeToFile("File content".getBytes(), file);
writeToFile
publicstaticvoidwriteToFile(byte[] data, File file)throws IOException {
BufferedOutputStreambos=null;
try {
FileOutputStreamfos=newFileOutputStream(file);
bos = newBufferedOutputStream(fos);
bos.write(data);
}
finally {
if (bos != null) {
try {
bos.flush ();
bos.close ();
}
catch (Exception e) {
}
}
}
}
Don't forget to add following permission in AndroidManifest.xml
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Solution 2:
Here is a... nearly complete class for writing to the documents folder on the emulated external storage of the device.
Don't forget to add the write permissions to manifest.
Post a Comment for "Android Write To File Not Working"