I am creating a sample application in my android phone to create a new text file and write some data on to that text file.
4
- which type of data?Divyesh Patel– Divyesh Patel2016-12-09 06:57:47 +00:00Commented Dec 9, 2016 at 6:57
- Same like java. Just you have to mention where to keep that file. e.g. Environment.getExternalStorageDirectory()+"/text/"Kush Patel– Kush Patel2016-12-09 06:58:34 +00:00Commented Dec 9, 2016 at 6:58
- Normal string dataGaurav K– Gaurav K2016-12-09 09:52:43 +00:00Commented Dec 9, 2016 at 9:52
- What exactly does Environment.getExternalStorageDirectory() doGaurav K– Gaurav K2016-12-09 09:53:44 +00:00Commented Dec 9, 2016 at 9:53
Add a comment |
1 Answer
Use these code you can write a text file in SDCard along with you need to set permission in android manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> for device having os >= LOLIPOP you need to ask permissions runtime, this is the code :
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(); } } 1 Comment
Aravindh Gopi
copy cat stackoverflow.com/a/8152217/5289704