When trying to create a simple .txt or .xml external file (NOT on SD card), my app is throwing a FileNotFoundException. If I let the system pick the path (returns /storage/emulated/legacy) it throws EACCES (permission denied). Same as when I set the path manually to "/android/data" (returns /storage/emulated/0/android/data) or "/download" (returns /storage/emulated/0/Download) - both throw EACCES. If I set path to "/document" it throws ENOENT (no such file or directory).
As see below in my code, I do a check before to make sure that external storage is available and not read-only.
I ran this on 2 devices, running 4.4.2 and 4.4.4 I also added "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" in the manifest as suggested in SO answers to try and force the device to use it's own file system and not the PC's when running app through USB, but it seems to keep giving me a path derived from /emulated/ I try debugging from the device itself using the developer options and thus eliminating the possibility of the app accessing the PC's storage, but the debugger on the device just hangs trying to attach.
Of course I have as well: "android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"
This is very frustrating as there are some similar questions on SO but with very accepted answers.
public static File getExternalStorageDirectory() {
Log.i("EXTERNAL STORAGE DIR", EXTERNAL_STORAGE_DIRECTORY.toString());
return EXTERNAL_STORAGE_DIRECTORY;
}
public static final File EXTERNAL_STORAGE_DIRECTORY = getDirectory("EXTERNAL_STORAGE", "android/data");
public static File getDirectory(String variableName, String defaultPath) {
String path = System.getenv(variableName);
return path == null ? new File(defaultPath) : new File(path);
}
public boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
return true;
}
return false;
}
public boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
public void writeToSettingsFile(String name, String value){
// File myDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "/settings.txt");
// File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), fileUrl);
// File myDir = getDirectory("EXTERNAL_STORAGE", "android/data");
// File myDir = getExternalStorageDirectory();
// File myDir = new File("download");
File myDir = new File(Environment.getExternalStorageDirectory() + "/android/data");
if(!myDir.exists()){
myDir.mkdirs();
}
try{
String fname = "settings.txt";
File file = new File (myDir, fname);
FileOutputStream fOut = new FileOutputStream(file);
Toast.makeText(context, "you made it, fOut!!!!", Toast.LENGTH_LONG).show();
props.setProperty(name, value);
props.store(fOut, "Settings Properties");
// props.storeToXML(fOut, "Settings Properties");
fOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e("WTF", "No file found!");
}
catch (IOException e) {e.printStackTrace();
}
}