Saving file to phone instead of SD card

孤者浪人 提交于 2019-12-20 02:35:22

问题


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.


回答1:


Got it by changing

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

into

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



回答2:


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




回答3:


Try andriods Data-Storage.

Hope this helps.




回答4:


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



来源:https://stackoverflow.com/questions/4678544/saving-file-to-phone-instead-of-sd-card

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!