将文件保存到手机而不是 SD 卡

发布于 2024-10-11 23:12:07 字数 459 浏览 4 评论 0原文

在我的应用程序中,我通过执行以下操作将 XML 文件保存到用户的 SD 卡 File newxmlfile = new File(Environment.getExternalStorageDirectory() + "/Message.xml");

但并非所有用户都有 SD 卡在他们的手机中,因此我的应用程序可能会崩溃。

我必须如何更改文件创建方法才能将文件保存到手机内存而不是 SD 卡上?

另外,我必须如何更改文件的加载? (当前: new InputSource(new FileInputStream(Environment.getExternalStorageDirectory() + "/Message.xml"))

编辑:我认为您的问题不正确。我知道 Android 文档的数据存储页面并且我已经阅读过它。我只是想知道应该如何调整它以匹配我当前保存和加载文件的方式。

In my app I save an XML file to the user's SD card by doing File newxmlfile = new File(Environment.getExternalStorageDirectory() + "/Message.xml");

But not all users have SD-cards in their phone and therefore my app is likely to crash.

How must I change my File creating method in order to save the file to the phone's memory instead of the SD-card?

Also, how must I change the loading of the file? (currently: new InputSource(new FileInputStream(Environment.getExternalStorageDirectory() + "/Message.xml")))

EDIT: I don't think you got the question right. I know the Data-Storage page of the Android Docs and I've read it. I just want to know how I should it adjust it to match my current way of saving and loading a file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逆蝶 2024-10-18 23:12:07

通过改变成得到

Environment.getExternalStorageDirectory() + "/Message.xml"

Environment.getDataDirectory() + "/data/com.companyname.appname/files/Message.xml"

Got it by changing

Environment.getExternalStorageDirectory() + "/Message.xml"

into

Environment.getDataDirectory() + "/data/com.companyname.appname/files/Message.xml"
琴流音 2024-10-18 23:12:07

首先,您应该通过检查 getExternalStorageState() 来检查外部存储(SD 卡)是否可用并准备好写入。
在这里阅读更多内容: http://developer.android.com/guide /topics/data/data-storage.html#filesExternal

然后,如果 SD 卡不可用,您可以写入内部存储。
在这里阅读更多信息,例如如何编写和阅读: http:// /developer.android.com/guide/topics/data/data-storage.html#filesInternal

First you should check if External Storage (SD card) is available and ready to write to by checking getExternalStorageState().
Read more here: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

Then if SD card not available you can write to internal storage.
Read more here for example on how to write and read: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

随风而去 2024-10-18 23:12:07

尝试使用 andriods 数据存储

希望这有帮助。

Try andriods Data-Storage.

Hope this helps.

零度° 2024-10-18 23:12:07

您可以使用 openFileOutput 获取访问权限到内部存储器。有关详细信息,您应该阅读有关内部存储的文档

You can use openFileOutput to get an access to the internal storage. For more info, you should read the documentation on internal storage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文