Here is the code I'm running:
File root = android.os.Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File dir = new File(root.getAbsolutePath());
dir.mkdirs();
File file = new File(dir, "myData.txt");
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println("Hi , How are you");
pw.println("Hello");
pw.flush();
pw.close();
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
android.util.Log
.i("tag",
"******* File not found. Did you add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
} catch (IOException e) {
e.printStackTrace();
}
The code executes without issues, and if I run these tests, all show that file is present on the file system:
File testFile = new File(dir, "myData.txt");
Boolean exists = testFile.exists();//true
Boolean hidden = testFile.isHidden();//false
String[] files = dir.list();//list contains the file
BUT I can't see the file in File Explorer or Root Browser or Terminal Emulator. AND the list of files queried by dir.list()
is different from what I see in Root Browser in /mnt/sdcard/Downloads
. I tried searching for the file myData.txt
in Terminal Emulator with find / -name *myData*
with no results.
I'm running the code on a rooted s4, the AndroidManifest contains WRITE_EXTERNAL_STORAGE permission.
Why is this happening and how do I get the user to see the file after it has been created?