Saving to SD card in KitKat

谁都会走 提交于 2019-12-02 11:09:06

It worked fine

Only for devices where /mnt/extSdCard existed and was writeable. This is outside the bounds of the Android SDK, and there is no guarantee that any device will honor that specific path. Perhaps you were in a controlled environment, with a single device model, where making this assumption was (relatively) safe.

Is there a workaround?

As 323go mentions in the comments, you are welcome to use getExternalFilesDirs() (note the plural). If there is more than one element in the resulting list, the second and subsequent entries will be for "secondary external storage" such as removable cards. You can read and write from this directory without any required permissions.

There is also ContextCompat, which offers a getExternalFilesDirs() implementation that works all the way back to API Level 4. It will only ever return one value on devices running less than API Level 19, but it allows you to skip version checking yourself, as plenty of API Level 19+ devices will also only return one value (e.g., they do not have an SD card slot populated with a card).

I recently wrote a series of blog posts attempting to clarify the storage situation. You may be particularly interested in the one on removable storage.

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