0

how to write a text file in a public storage that it was visible for other apps. i.e. file manager. what is wrong with my code.

String filename = "kontaktebi123.vcf";
    FileOutputStream outputStream;

    try {
        outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
        PrintWriter osw = new PrintWriter(outputStream);
        ArrayList<String> nomrebi = numberGenerator(view);
        osw.printf("blablabla")
        osw.flush();
        osw.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
mrsamkhar
  • 77
  • 8
  • Refer this link... https://stackoverflow.com/questions/8152125/how-to-create-text-file-and-insert-data-to-that-file-on-android – Sachin Soma Jul 16 '18 at 06:17

2 Answers2

2

You have to write the file in public directory like Document, Download or others.

String fileDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
String fileName = "kontaktebi123.vcf";

File file = new File(fileDir + "/" + fileName);

Now write in the file and it will be visible to other apps.

0
try{
FileOutputStream fileOutStream =  openFileOutput(fileName.txt",MODE_PRIVATE);
OutputStreamWriter outputWriter = new OutputStreamWriter(fileOutStream);
outputWriter.write("write your text here");
outputWriter.close();
Toast.maketext(context,"File Saved Successfully",Toast.LENGTH_SHORT).show();
}catch(Exception e){
e.printStackTrace();
}
Ankita
  • 1,129
  • 1
  • 8
  • 15