gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:39:58 24 4
gpt4 key购买 nike

我正在尝试将文本文件导出到 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());
}
}
}

最佳答案

你的问题是这样的:

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

关于android - 将文本文件导出到 SD 卡或外部存储器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49789825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com