Dave's Brain

Browse - Programming Tips - How do I write to the SD card?

Date: 2016aug22
OS: Android
Keywords: secure digital

Q.  How do I write to the SD card?

A.	Like this:

// Helper function to get the path String getExternalStorage() { return Environment.getExternalStorageDirectory().getAbsolutePath(); } <!-- Add this to your manifest --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 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() }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.