Date: 2016aug22
OS: Android
Keywords: secure digital
Language: mixed
Q. Android: How to write to the SD card?
A. Like this:
Add this to your manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Java code:
// Helper function to get the path
String getExternalStorage() {
return Environment.getExternalStorageDirectory().getAbsolutePath();
}
void writeOnSdcard() {
String dir = getExternalStorage() + "/something-unique";
File fDir = new File(dir);
if (!fDir.exists()) {
fDir.mkdirs();
}
String file = dir + "/myfile.txt";
// Now its just regular Java I/O
PrintWriter writer = new PrintWriter(file, "UTF-8");
writer.println("Hello");
writer.println("SD Card");
writer.close()
}