【问题标题】:Write text file in the device's storage [duplicate]在设备的存储中写入文本文件[重复]
【发布时间】:2018-07-16 05:59:28
【问题描述】:

如何在公共存储中写入对其他应用可见的文本文件。即文件管理器。 我的代码有什么问题。

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();
    }

【问题讨论】:

标签: android


【解决方案1】:

您必须将文件写入公共目录,如DocumentDownload 或其他。

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

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

现在写入文件,它将对其他应用可见。

【讨论】:

    【解决方案2】:
    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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-13
      • 2014-09-05
      相关资源
      最近更新 更多