How to have Read + Write external storage after targeting android O (26) 8.0

白昼怎懂夜的黑 提交于 2020-01-02 05:23:16

问题


Before targetting android O code below worked perfectly. After setting targetApi=27 (8.1) this stopped working.

context.requestPermissions(new String[]{
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        requestCode);

I am presented with a permission dialog we all know, I gladly allow App to access photos media and files on a device.

and in onRequestPermissionResult() it returns

android.permission.READ_EXTERNAL_STORAGE == 0, 
android.permission.WRITE_EXTERNAL_STORAGE == -1

I followed this answer https://stackoverflow.com/a/48353465/4858147 didn't work.

While I was looking for a solution, found this https://developer.android.com/about/versions/oreo/android-8.0-changes.html#rmp I wasn't cleverer after reading this, but I am 100% sure this is causing it.

If the app targets Android 8.0 (API level 26), the system grants only READ_EXTERNAL_STORAGE at that time; however, if the app later requests WRITE_EXTERNAL_STORAGE, the system immediately grants that privilege without prompting the user.

I tried asking only for read permission (dialog-> allow) and later I asked for write permission (no dialog -> still not granted) it didn't work.

manifest:

<!-- Internet Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!-- I added now, -->
<uses-permission android:name="android.permission.CAMERA"/>

<!-- Storage Permissions -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<!-- Account Access Permission -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<!-- Vibrate used for notification updates -->
<uses-permission android:name="android.permission.VIBRATE" />

I need permission for this

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra("output", outputDestination);

回答1:


Okay so I finally know what's happening, after reading https://stackoverflow.com/a/49709872/4858147 I was finally able to continue.

Problem was that HockeyApp had in manifest:

<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="18" />

and after manifests were merged there was no WRITE_EXTERNAL_STORAGE permission in app. The app worked before thanks to a bug in an android system

Prior to Android 8.0 (API level 26), if an app requested a permission at runtime and the permission was granted, the system also incorrectly granted the app the rest of the permissions that belonged to the same permission group, and that were registered in the manifest.

It worked before thanks to bug in Android system, because we asked for read permission

All I did was that in my main App manifest I changed

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    tools:remove="android:maxSdkVersion" />


来源:https://stackoverflow.com/questions/49775322/how-to-have-read-write-external-storage-after-targeting-android-o-26-8-0

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