0

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? Commented Dec 9, 2016 at 6:57
  • Same like java. Just you have to mention where to keep that file. e.g. Environment.getExternalStorageDirectory()+"/text/" Commented Dec 9, 2016 at 6:58
  • Normal string data Commented Dec 9, 2016 at 9:52
  • What exactly does Environment.getExternalStorageDirectory() do Commented Dec 9, 2016 at 9:53

1 Answer 1

9

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(); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.