保存文件到Android内部存储器

huangapple go评论41阅读模式
英文:

Save file to internal storage in Android

问题

我想制作一个简单的Android应用程序,用户可以在其中编写文本,当用户按下保存按钮时,我将从文本区域获取文本,然后创建文件夹并将文件保存到存储中。
我已经在外部存储中完成了这个任务,下面是我的代码:

String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/newfolder";
File f = new File(path);
f.mkdir();
PrintWriter pw = new PrintWriter(path + "/" + txt_name.getText() + ".txt");
pw.write(main_txt.getText().toString());
pw.close();

这段代码在具有外部内存卡的设备上运行良好,但我想要在内部存储中保存文件,以便该代码可以在任何设备上运行。如何将文件保存到内部存储中?

英文:

I want to make a simple Android application which will make user write text and when he presses on save button I'll get the text from text area and create folder then save file into folder in the storage.
I did that but in external storage and here's my code

String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/newfolder";
                File f = new File(path);
                f.mkdir();
                PrintWriter pw = new PrintWriter(path + "/" + txt_name.getText() + ".txt");
                pw.write(main_txt.getText().toString());
                pw.close();

This code runs well but it runs only on devices which have an external memory card, so I want to do the same but save file on internal storage for device to make this code run on any device, how can I save file into internal storage?

答案1

得分: 0

你的代码保存在手机内置的外部存储器中。

你的代码与可移动的微型SD卡无关。

英文:

No.

Your code saves to external memory which is build into the phone.

Your code has nothing to do with a removable micro SD card.

huangapple
  • 本文由 发表于 2020年7月30日 12:38:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63166284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定