Currently I am working in project where I need to parse a remote text file and store it in local storage (internal/external). I am able to parse the text file but unable to
First thing, your AndroidManifest.xml
file is correct. So no need to correct that. Though I would recommend organizing your permissions in one place.
FileOutputStream fOut = openFileOutput("myinternalfile.txt",MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
//---write the contents to the file---
osw.write(strFileData);
osw.flush();
osw.close();
//This will get the SD Card directory and create a folder named MyFiles in it.
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/MyFiles");
directory.mkdirs();
//Now create the file in the above directory and write the contents into it
File file = new File(directory, "mysdfile.txt");
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(strFileData);
osw.flush();
osw.close();
Note: If running on an emulator, ensure that you have enabled SD Card support in the AVD properties and allocated it appropriate space.
Replace this lines
File myFile = new File("sdcard/mysdfile.txt");
if(!myFile.exists()){
myFile.mkdirs();
}
myFile = new File("sdcard/mysdfile.txt");
with
//Saving the parsed data.........
File myFile = new File(Environment.getExternalStorageDirectory()+"/mysdfile.txt");
myFile.createNewFile();
File myFile = new File("sdcard/mysdfile.txt");
Should be changed to
File myFile = new File("/mnt/sdcard/mysdfile.txt");