How to store a file on an SD card (external)

早过忘川 提交于 2019-12-02 08:57:53

问题


I want to store a file on the SD card using File. I already achieved to store a file in external storage (named internal storage in my file explorer app. This is NOT the SD card).

I read much about storing data on SD cards and often saw:

new File(Environment.getExternalStorageDirectory() + "/MyFile")

But this will store the file on external storage and not on the SD card. So how can I store a file on the SD card and not on external storage?


回答1:


Ideally, you use the Storage Access Framework (e.g., ACTION_CREATE_DOCUMENT) and let the user choose where the user wants to put this content. The user may wish to put the content somewhere else. For example, the user may not have removable storage.

Temporarily, you are welcome to use getExternalFilesDirs() and similar methods on Context. If these return 2+ elements, the second and subsequent ones are locations on removable storage. On Android 4.4+, your app can write to those locations without any particular permissions. Android Q is changing the rules here a lot, which is another reason to use the Storage Access Framework instead.




回答2:


Using this may help you:

An answer to Stack Overflow question How do I get the external storage's path?

Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)


来源:https://stackoverflow.com/questions/55982246/how-to-store-a-file-on-an-sd-card-external

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