将文件保存到手机而不是 SD 卡
在我的应用程序中,我通过执行以下操作将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过改变成得到
它
Got it by changing
into
首先,您应该通过检查 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
尝试使用 andriods 数据存储。
希望这有帮助。
Try andriods Data-Storage.
Hope this helps.
您可以使用
openFileOutput
获取访问权限到内部存储器。有关详细信息,您应该阅读有关内部存储的文档。You can use
openFileOutput
to get an access to the internal storage. For more info, you should read the documentation on internal storage.