I tried to create a text file and write data to it using the code shown below:
JAVA CODE_1:
File dir = Environment.getExternalStorageDirectory();
File file = new File(dir,"/DCIM/"+fileTitle);
try {
if( file.exists() ){
FileOutputStream fos = openFileOutput(fileTitle, MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write("this is a text");
osw.flush();
osw.close();
} else {
file.createNewFile();
FileOutputStream fos = openFileOutput(fileTitle, MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write("this is a text");
osw.flush();
osw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But when I check the DCIM directory, I find that the file is created but no data is written to it. please tell me where my mistake is.
Sorry, Its not created on External Storage but its created on Application’s Internal Storage,
(Check your file is created on
/data/data/<Application_Package_Name>/filesdirectory)Because,
you are using
openFileOutputwhich one creates file in Application’s Internal storage.Since: API Level 1
Open a private file associated with this Context’s application package for writing. Creates the file if it doesn’t already exist.
Code: