将文本文件导出到SD卡或外部存储器

时间:2018-04-12 06:50:56

标签: android

尝试将文本文件导出到SD卡中的用户定义文件夹。但它将文件保存到SD卡中的默认文件夹(即本地 - > SD卡 - > Android - >数据)。

此处包含我的MainActivity。请帮我识别我的缺陷。

public class MainActivity extends AppCompatActivity {
  EditText editText;
  private int STORAGE_PERMISSION_CODE = 23;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = (EditText) findViewById(R.id.editText2);
  }

  @RequiresApi(api = Build.VERSION_CODES.KITKAT)
  public void savePrivate(View view) {
    String info = editText.getText().toString();
    File folder = getExternalFilesDir("sample"); // Folder Name
    File myFile = new File(folder, "myData2.txt"); // Filename
    writeData(myFile, info);
    editText.setText("");
    createFile(info);

  }
  private void writeData(File myFile, String data) {
    FileOutputStream fileOutputStream = null;
    try {
      fileOutputStream = new FileOutputStream(myFile);
      fileOutputStream.write(data.getBytes());
      Toast.makeText(this, "Done" + myFile.getAbsolutePath() +
        "sample/", Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (fileOutputStream != null) {
        try {
          fileOutputStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }

  @RequiresApi(api = Build.VERSION_CODES.KITKAT)
  private void createFile(String text) {
    File root = null;
    File[] files = null;
    try {
      files = getExternalFilesDirs(Environment.DIRECTORY_DOCUMENTS);
      //Getting the path of the sdcard
      //Android/data/com.demo.sdcardandroid/files/Documents
      root = files[1];
      Log.i("SDCARD", "path.." + root.getAbsolutePath());
      //check sdcard permission
      if (root.canWrite()) {
        File fileDir = new File(root.getAbsolutePath());
        fileDir.mkdirs();

        File file = new File(fileDir, "samplefile.txt");
        FileWriter filewriter = new FileWriter(file);
        BufferedWriter out = new BufferedWriter(filewriter);
        out.write(text);
        out.close();
      }
    } catch (IOException e) {
      Log.e("ERROR:---", "Could not write file to SDCard" + e.getMessage());
    }
  }
}

1 个答案:

答案 0 :(得分:0)

你的问题是:

getExternalFilesDirs(Environment.DIRECTORY_DOCUMENTS);

您应该使用:

Environment.getExternalStorageDirectory();

以下是createFile方法的外观:

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void createFile(String text) {
        File root = null;
        File path = null;
        try {
            path = Environment.getExternalStorageDirectory();
            //Getting the path of the sdcard
            //Android/data/com.demo.sdcardandroid/files/Documents
            Log.i("SDCARD", "path.." + root);
            //check sdcard permission
            if (path.canWrite()) {
                File file = new File(path, "samplefile.txt");
                FileWriter filewriter = new FileWriter(file);
                BufferedWriter out = new BufferedWriter(filewriter);
                out.write(text);
                out.close();
            }
        } catch (IOException e) {
            Log.e("ERROR:---", "Could not write file to SDCard" + e.getMessage());
        }
    }