You need to check the permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Below is an example that saves text file on device.
public void generateNoteOnSD(Context context, String sFileName, String sBody) {
try {
File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, sFileName);
FileWriter writer = new FileWriter(gpxfile);
writer.append(sBody);
writer.flush();
writer.close();
Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
Just call him that way:
generateNoteOnSD(this, "nome_do_arquivo", "texto_do_arquivo");
What I need to change in this code to save in the internal memory of the mobile? my mobile has no SD card.
– Roberto Albino
https://developer.android.com/training/basics/data-storage/files.html This link can help you, explain internal and external storage.
– LucasMotta