How to correctly use getExternalStorageDirectory () to create text files that can be viewed by the user. Regardless of whether I have an external memory or not.
How to correctly use getExternalStorageDirectory () to create text files that can be viewed by the user. Regardless of whether I have an external memory or not.
Every Android device has shared external storage, either an SD memory, or virtualized. Simply use the external memory system to store your data, an example could be:
try {
File nuevaCarpeta = new File(Environment.getExternalStorageDirectory(), "CarpetaDePrueba");
if (!nuevaCarpeta.exists()) {
nuevaCarpeta.mkdir();
}
try {
File file = new File(nuevaCarpeta, "Archivo" + ".txt");
file.createNewFile();
} catch (Exception ex) {
Log.e("Error", "ex: " + ex);
}
} catch (Exception e) {
Log.e("Error", "e: " + e);
}
Without forgetting the permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />