Android 我是否必须同时声明 WRITE_EXTERNAL_STORAGE 和 READ_EXTERNAL_STORAGE?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11942400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:15:13  来源:igfitidea点击:

Do I have to declare both WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE?

androidpermissionsandroid-sdcard

提问by Jeff Axelrod

Is it enough to declare <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />or do I also have to declare <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />? The Javadocs omit this important information.

申报就够了<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />还是我也必须申报<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />?Javadocs 忽略了这一重要信息。

采纳答案by wsanville

READ_EXTERNAL_STORAGEonly exists as of Jelly Bean (Level 16). According to the docs, all applications as of Jelly Bean have that permission, even without declaring it:

READ_EXTERNAL_STORAGE只存在于 Jelly Bean(16 级)。根据docs,所有 Jelly Bean 的应用程序都具有该权限,即使没有声明它:

Provides protected read access to external storage. In Android 4.1 by default all applications still have read access. This will be changed in a future release to require that applications explicitly request read access using this permission. If your application already requests write access, it will automatically get read access as well. There is a new developer option to turn on read access restriction, for developers to test their applications against how Android will behave in the future.

提供对外部存储的受保护读取访问。默认情况下,在 Android 4.1 中,所有应用程序仍然具有读取权限。这将在未来版本中更改,以要求应用程序使用此权限显式请求读取访问权限。如果您的应用程序已经请求写访问权限,它也会自动获得读访问权限。有一个新的开发人员选项可以打开读取访问限制,以便开发人员根据 Android 未来的行为测试他们的应用程序。

So, you should declare it for future compatibility, but this might not be the source of your problem, unless you're using a Jelly Bean phone and set the developer option "Protect USB storage" option.

因此,您应该声明它以备将来兼容,但这可能不是问题的根源,除非您使用的是 Jelly Bean 手机并设置了开发人员选项“保护 USB 存储”选项。

回答by Christopher Orr

It's best to be explicit and declare both permissions, but declaring only android.permission.WRITE_EXTERNAL_STORAGEwill automatically add android.permission.READ_EXTERNAL_STORAGEto your APK at build time.

最好是明确的并声明这两个权限,但仅声明android.permission.WRITE_EXTERNAL_STORAGEandroid.permission.READ_EXTERNAL_STORAGE在构建时自动添加到您的 APK 中。

You can use the command aapt dump badgingon an APK to see that Android considers usage of the write permission to implythat you also want read permission.

您可以aapt dump badging在 APK 上使用该命令来查看 Android 是否考虑使用写入权限来暗示您也需要读取权限。

Here's some output from aapt for an APK of mine where I declared onlyWRITE_EXTERNAL_STORAGEin my manifest:

这是 aapt 的一些输出,用于我的 APK,我WRITE_EXTERNAL_STORAGE在清单中声明:

uses-permission:'android.permission.WRITE_EXTERNAL_STORAGE'
uses-permission:'android.permission.READ_EXTERNAL_STORAGE'
uses-implied-permission:'android.permission.READ_EXTERNAL_STORAGE',
  'requested WRITE_EXTERNAL_STORAGE'