I am working on an app for android that will create a new .txt
file on the SDcard mounted to the phone. I am new to android programming but I wrote this code. And then I tested the app on a real phone(not emulator). I navigated to My Files>All files>SD memory card and the file that is supposed to be created is not there.
What am I doing wrong and how to fix my error? Thanks in advance !
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String filename = "filename.txt";
File file = new File(Environment.getExternalStorageDirectory(), filename);
FileOutputStream fos;
byte[] data = new String("data to write to file").getBytes();
try {
fos = new FileOutputStream(file);
fos.write(data);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// handle exception
} catch (IOException e) {
// handle exception
}
UPDATE !!!
I found the files created in the phone's memory for some reason. Why this happened and how to create them in the memory card instead?