I want to create a text file in a folder in sdcard then write some data in that file. I check lots of pages like:
Saving to SD card as text file
Write file to sdcard in android
I use this code to create file and folder:
try {
File newFolder = new File(Environment.getExternalStorageDirectory(), "TestFolder");
if (!newFolder.exists()) {
newFolder.mkdir();
}
Log.d(TAG, "TestFolder");
try {
file = new File(newFolder, "MyTest" + ".txt");
file.createNewFile();
Log.d(TAG, "MyTest");
} catch (Exception ex) {
System.out.println("ex: " + ex);
}
} catch (Exception e) {
System.out.println("e: " + e);
}
However it creates file in phone memory not sdcard. I used these permissions: Then I used this code to write a small string in the same text file:
String str = "my_string";
FileOutputStream outStream;
try {
outStream = openFileOutput(file.getName(), Context.MODE_PRIVATE);
outStream.write(str.getBytes());
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
But it doesn't write any thing in the text file file. What's wrong with my codes?
By the way I used these permissions too: READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE",READ_INTERNAL_STORAGE",WRITE_INTERNAL_STORAGE"
So it is not the matter of permission.