My application should create files that can then be easily read / written / copied from a connected PC. I read everything I can find and tried everything that was recommended by the online comments and Android docs, but I just can't get it to work.
I successfully create and write files. Using the ADB shell, I see that there are files, but from (for example) Windows I can see every file in the EXCEPT directory of the ones I create. Files copied to an Android (say) Windows device are publicly available. But the files created by my applications, even if not in the same directory. Since I know there are files, but visibility is selective, I believe this is a permission issue.
Digging more, I found Context.MODE_WORLD_READABLE and _WRITABLE, but "this constant was deprecated at API level 17", so this approach is clearly not suitable. I also tried File.setReadable and File.setWritable in existing files, but both fail and return false.
Yes, the application has "android.permission.WRITE_EXTERNAL_STORAGE" (or it will not be written to external storage). It can create files, they simply are not public.
I could not find any file creation methods that have any condition for explicitly creating public files, except for files and contexts that have problems, as mentioned above. It seems that there have been many ways to do this over the years, but they are either outdated, or do not work, or I do not use them correctly.
So ... What is the correct and supported way to create public files on Android external storage? Thanks!
Edit: code added. Nothing unusual, just proof of the conceptual material at the moment. Again, this file successfully creates Android files in the specified directory ... but these files are not visible to external devices when connected via USB, either "as a camera" or "as a media device". All other directories and files are visible to the external device, and not those created by my application.
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
filename = (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/test.txt");
try {
writer = new FileWriter(filename);
} catch (IOException e) {
e.printStackTrace();
}
try {
writer.write(filename);
} catch (IOException e) {
e.printStackTrace();
}
try {
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}