我是否需要运行时权限检查才能写入棉花糖中的getExternalFilesDir()路径? [英] Do I need runtime permission check to write into getExternalFilesDir() path in marshmallow?

查看:122
本文介绍了我是否需要运行时权限检查才能写入棉花糖中的getExternalFilesDir()路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的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?

推荐答案

从KITKAT开始,不需要任何权限来读取或写入返回的路径;调用应用程序始终可以访问它.这个仅适用于为调用程序包名称生成的路径应用.要访问属于其他软件包的路径,WRITE_EXTERNAL_STORAGE和/或READ_EXTERNAL_STORAGE是必需的.

Starting in KITKAT, no permissions are required to read or write to the returned path; it's always accessible to the calling app. This only applies to paths generated for package name of the calling application. To access paths belonging to other packages, WRITE_EXTERNAL_STORAGE and/or READ_EXTERNAL_STORAGE are required.

您将拥有对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()路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆