Create package directory in ../Android/data denied on SD-Cards on Android >4.4

对着背影说爱祢 提交于 2020-01-25 10:30:06

问题


I have trouble creating the "com.foo.Example" folder of my app in "/storage/extSdCard/Android/data" from inside the app. If created manually with a file manager there is no trouble, just creating the first one with a simple mkdir returns false. I used the package name declared in the manifest and used throughout and I do have the appropriate WRITE_EXTERNAL_STORAGE-permission in there aswell. Where is this folder suposed to come from, if the app is unable to create it?


回答1:


I'm assuming you're using getExternalFilesDir(), if not, you'll want to convert to it. getExternalFilesDir(), when passed null, will create a private directory for your application to write to if it doesn't already exist and return the handle for that.

You'll want to check out this guide, which has all the info you need on how to write to external storage.

As for why you're having this issue on KitKat, Google changed the permission model for external storage to prevent apps from writing to arbitrary folders on the SD card, which provides better security, as well as keeping things better organised. However, the cool part is that getExternalFilesDir() doesn't even require a permission to use! It's always available to your application.

Hope that helps!



来源:https://stackoverflow.com/questions/26634991/create-package-directory-in-android-data-denied-on-sd-cards-on-android-4-4

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