问题描述
在我的android应用中,我将一些文件保存到 Environment.getExternalStorageDirectory()+"\ MyApp"
目录中.直到android 6棉花糖更新,这工作正常.棉花糖更新后,我无法写入该目录.
In my android app, I save some files to Environment.getExternalStorageDirectory() + "\MyApp"
directory. This worked fine until android 6 marshmallow update. After marshmallow update, I cannot write to this directory.
如此答案中所述,在棉花糖中,应用程序需要在运行时向用户请求权限,然后才能写入外部存储.
As described in this answer, in marshmallow, apps need to ask for the permission from user at runtime before writing to external storage.
但是,当我使用 context.getExternalFilesDir(null)
而不是 Environment.getExternalStorageDirectory()
时,我不需要在运行时请求任何许可,可以正常工作(从 context.getExternalFilesDir(null)
返回的路径也在外部存储目录中).
But, when I use context.getExternalFilesDir(null)
instead of Environment.getExternalStorageDirectory()
, I don't need to ask for any permission at runtime and it just works (path returned from context.getExternalFilesDir(null)
is also inside the external storage directory).
这是巧合还是我可以继续写 context.getExternalFilesDir(null)
而无需在运行时征得许可?
Is this some kind of a coincidence or can I continue to write to context.getExternalFilesDir(null)
without asking permission at runtime?
推荐答案
文档指出:
您将拥有对Android 4.4+上的 getExternalFilesDir()
的读/写访问权限,而无需任何权限.
You will have read/write access to getExternalFilesDir()
on Android 4.4+ without requiring any permissions.
如果您愿意,我建议使用 FileProvider 需要支持较低的API级别.
I would recommend using a FileProvider if you need to support lower API levels.
这篇关于我是否需要运行时权限检查才能写入棉花糖中的getExternalFilesDir()路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!