定位android O之后如何读取和写入外部存储(26)8.0 [英] How to have Read + Write external storage after targeting android O (26) 8.0

查看:402
本文介绍了定位android O之后如何读取和写入外部存储(26)8.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在定位下面的Android O代码之前,效果很好。设置targetApi = 27(8.1)后,此操作停止工作。

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);

我看到了一个众所周知的权限对话框,很高兴允许App访问照片媒体和文件

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

并在 onRequestPermissionResult()中返回

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

我遵循了这个答案 https://stackoverflow.com/a/48353465/ 4858147 无法正常工作。

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

我在寻找解决方案时,发现了 https://developer.android.com/about/versions/oreo/android-8.0-changes.html#rmp 阅读此书后我并不聪明,但我100%确信这是造成这种情况的原因。

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.


如果应用程序的目标是Android 8.0(API级别26),则系统仅授予
当时的READ_EXTERNAL_STORAGE;但是,如果应用程序稍后请求
WRITE_EXTERNAL_STORAGE,则系统会立即授予该特权
,而不会提示用户。

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.

我尝试只请求读取权限( dialog-> allow ),后来又请求写入权限(没有对话框->仍未授予)它不起作用。

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.

清单:

<!-- 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" />

我需要对此权限

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


推荐答案

好吧,在阅读< a href = https://stackoverflow.com/a/49709872/4858147> https://stackoverflow.com/a/49709872/4858147 我终于能够继续。

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

问题是HockeyApp出现在清单中:

Problem was that HockeyApp had in manifest:

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

清单合并后,应用程序中没有WRITE_EXTERNAL_STORAGE权限。

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


在Android 8.0(API级别26之前)之前,该应用可以正常运行,前提是该应用请求了权限
在运行时获得了权限,系统也错误地将
授予了属于同一
权限组且已在清单中注册的其余权限。 p>

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.

在Android系统中出现错误之前就可以使用了,因为我们要求读取权限

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" />

这篇关于定位android O之后如何读取和写入外部存储(26)8.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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