In Android I declare this permission and request the permission, user also grant this permission.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Then I have a such method to get the folder which I can use to write to.
<string name="root_folder" translatable="false">MyRootFolder</string>
public static File getFolder(Context ctx) {
File folder;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) {
folder = Environment.getExternalStoragePublicDirectory(ctx
.getString(R.string.root_folder));
} else {
folder = new File(ctx.getFilesDir(), ctx.getString(R.string.root_folder));
}
if (!folder.exists()) {
folder.mkdir();
}
return folder;
}
For most devices, this is working fine, But on some devices, I found the folder doesn't exist, so I cannot save the app's output to user's device.
folder.exists() is false, folder.canWrite() is false.
What i want to do is find a folder where I can save the data to user's device, if the External storage is not available, i can use cache folder by context.getFilesDir()