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.
问题:
回答1:
READ_EXTERNAL_STORAGE
only exists as of Jelly Bean (Level 16). According to the docs, all applications as of Jelly Bean have that permission, even without declaring it:
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.
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.
回答2:
It's best to be explicit and declare both permissions, but declaring only android.permission.WRITE_EXTERNAL_STORAGE
will automatically add android.permission.READ_EXTERNAL_STORAGE
to your APK at build time.
You can use the command aapt dump badging
on an APK to see that Android considers usage of the write permission to imply that you also want read permission.
Here's some output from aapt for an APK of mine where I declared only WRITE_EXTERNAL_STORAGE
in my manifest:
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'