在模拟器上没有显示READ_EXTERNAL_STORAGE权限请求

3

我正在尝试设置一种方式,在活动初始化后注册权限结果处理程序,通过创建一个 Map<Integer, Runnable> 来保存处理程序。当我请求权限时,我使用随机生成的代码,并保存可运行对象到代码/处理程序映射中。

以下是我在活动类中已经完成的部分内容:

private final HashMap<Integer, Runnable> onPermission = new HashMap<>();

//i call this from other classes to ask for a permission and register a handler
public void requestPermission(Runnable onGranted, String...permissions) {
    int code = Permissions.permissionRequestCode();
    onPermission.put(code, onGranted);
    requestPermissions(permissions, code);
}

//overriding this method to check if the Map has a handler for the granted request
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    Runnable handler = onPermission.get(requestCode);
    if (handler != null && isGranted(grantResults)) {
        handler.run();
        onPermission.remove(requestCode);
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

//utility method to check if all the requested permissions are granted
private boolean isGranted(int[] results) {
    for (int i : results) {
        if (i != PackageManager.PERMISSION_GRANTED) {
            return false;
        }
    }
    return true;
}

以下是一个示例,展示了如何使用它,其中owner是我的Activity实例:

private void readImages() {
    if (ContextCompat.checkSelfPermission(owner, Manifest.permission.READ_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
        loadImages();
    } else {
        owner.requestPermission(this::loadImages, Manifest.permission.READ_EXTERNAL_STORAGE);
    }
}

现在在实际设备上运行(Android 12,API 31),与我预期的完全一样(请求显示,并且在授予权限时执行处理程序),但是在模拟器上(Android 13,API 33),当请求权限时什么也不显示,它仍然是“未授予”的状态,即使它已经包含在清单中,权限也不会显示在“应用信息”中。

真实设备:

screenshot from the real device

模拟器:

screenshot from the emulator

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="Mesa"
        android:roundIcon="@mipmap/ic_launcher"
        android:supportsRtl="true"
        android:theme="@style/Theme.Mesa.NoActionBar"
        android:usesCleartextTraffic="true">
        <activity
            android:name=".app.Mesa"
            android:configChanges="uiMode|screenSize|colorMode"
            android:exported="true"
            android:theme="@style/Theme.Mesa.NoActionBar"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

My build.gradle :

android {
    compileSdk 33

    defaultConfig {
        applicationId "org.luke.mesa"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        proguardFiles 'proguard-rules.pro'
    }

    buildTypes {
        release {
            minifyEnabled true
        }

        debug {
            minifyEnabled false
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
        allprojects {
            tasks.withType(JavaCompile){
                options.compilerArgs <<"-Xlint:deprecation"
            }
        }
    }
    buildFeatures {
        viewBinding true
        dataBinding true
    }
    packagingOptions {
        resources {
            merges += ['META-INF/DEPENDENCIES']
        }
    }
    namespace 'org.luke.mesa'
    dependenciesInfo {
        includeInApk true
        includeInBundle true
    }
    buildToolsVersion '33.0.0'
}

可能的重复问题,但并未解决我的问题:


1
可能是这个吗?https://developer.android.com/about/versions/13/behavior-changes-13#granular-media-permissions "细粒度媒体权限" - Ken Wolf
1
天哪,我已经多次浏览了那个页面,认为它是“可选的”,现在我才注意到使用了“必须”的词语:您必须请求以下一个或多个细粒度媒体权限,而不是READ_EXTERNAL_STORAGE权限 - SDIDSA
1
是的,那就是问题所在。我刚刚尝试了READ_MEDIA_IMAGES,请求正确显示并执行了处理程序。非常感谢您。请考虑撰写一个答案,我会将其标记为已接受。 - SDIDSA
1个回答

4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接