Android API 33 permissions are not granted
Asked Answered
C

5

23

So as I read in the developer.android site the Android 13 brings new changes in the Permission field.

I have a Chat application where I need to make calls, open the camera, record audio, read and write in the external storage. So in the SplashScreen I request those permissions to the user. However some of them are not granted but without even the system ask me for those.

Here is what I have declared in the AndroidManifest.xml file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.gms.permission.AD_ID"/>
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

    <uses-feature
        android:name="android.hardware.camera"
        android:required="true" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:name=".ChatApplication"
        android:allowBackup="false"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="false"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/logo"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:networkSecurityConfig="@xml/network_security_config"
        android:requestLegacyExternalStorage="true"
        android:roundIcon="@mipmap/logo"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="AllowBackup,GoogleAppIndexingWarning,UnusedAttribute"
        tools:replace="android:allowBackup, android:label">

And here is what i have in the Splashscreen Activity

class SplashActivity : BaseActivity() {

    private val splashViewModel: SplashViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        if (!hasPermissions(this, *REQUIRED_PERMISSIONS))
            ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, ALL_PERMISSIONS)
        else
            Handler(Looper.getMainLooper()).postDelayed ({ startUi() }, 1000)
    }

    private fun hasPermissions(context: Context, vararg permissions: String): Boolean =
        permissions.all {
            ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
        }

    private fun startUi() {
        // navigate to main application
    }

    private fun finishActivity() {
        toast("You must grant all required permissions to continue")
        finish()
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            ALL_PERMISSIONS -> {
                if (grantResults.all { it == PackageManager.PERMISSION_GRANTED }) startUi()
                else finishActivity()
            }
        }
    }

    companion object {
        const val ALL_PERMISSIONS = 10
        private val REQUIRED_PERMISSIONS =
            mutableListOf (
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.CAMERA
            ).apply {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    add(Manifest.permission.READ_MEDIA_IMAGES)
                    add(Manifest.permission.READ_MEDIA_VIDEO)
                    add(Manifest.permission.READ_MEDIA_AUDIO)
                    add(Manifest.permission.POST_NOTIFICATIONS)
                } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
                    add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
            }.toTypedArray()
    }
}

When i put a breakPoint to the onRequestPermissionsResult i see this enter image description here

The Permission prompts that I see in the screen are for Camera, Record audio, and Phone State. After those the application exits by calling the finishActivity()

I'm running the app in a Pixel 6 Pro API 33 Emulator What is the proper way of doing that? How can i solve that?

Cheater answered 23/9, 2022 at 10:24 Comment(0)
P
15

According to log, may be you are missing 3 permission these are

 - READ_MEDIA_IMAGES
 - READ_MEDIA_VIDEO
 - READ_MEDIA_AUDIO

add instead of the above 3 permission in manifest

 <uses-permission android:name="android.permission.READ_MEDIA_*"/>

for getting all of the above three permission has taken only the external storage permission. In the android documentation on the link below, only these three permissions are provided
READ_MEDIA_IMAGES,
READ_MEDIA_VIDEO,
READ_MEDIA_AUDIO
instead of using READ_EXTERNAL_STORAGE.
link :https://developer.android.com/about/versions/13/behavior-changes-13#granular-media-permissions

Parfait answered 23/9, 2022 at 11:0 Comment(3)
can you please help me i have given all permission what you have mention here but still i am getting same errorHay
Thanks its worked it was my mistake i have not added conditionHay
How about reading PDF files?Rae
A
33

In Android 13, you do not need to ask for permissions

Here is an example of my code for passing permissions

in Mainfests.xml add

<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
      tools:ignore="ScopedStorage" />

Then, in your class or activity add :

public static String[] storage_permissions = {
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE
};

@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
public static String[] storage_permissions_33 = {
        Manifest.permission.READ_MEDIA_IMAGES,
        Manifest.permission.READ_MEDIA_AUDIO,
        Manifest.permission.READ_MEDIA_VIDEO
};

public static String[] permissions() {
    String[] p;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        p = storage_permissions_33;
    } else {
        p = storage_permissions;
    }
    return p;
}

When requesting permissions, call permissions();

 ActivityCompat.requestPermissions(MainActivity.this, 
                              permissions(), 
                              1);
Aerodrome answered 26/12, 2022 at 12:10 Comment(0)
P
15

According to log, may be you are missing 3 permission these are

 - READ_MEDIA_IMAGES
 - READ_MEDIA_VIDEO
 - READ_MEDIA_AUDIO

add instead of the above 3 permission in manifest

 <uses-permission android:name="android.permission.READ_MEDIA_*"/>

for getting all of the above three permission has taken only the external storage permission. In the android documentation on the link below, only these three permissions are provided
READ_MEDIA_IMAGES,
READ_MEDIA_VIDEO,
READ_MEDIA_AUDIO
instead of using READ_EXTERNAL_STORAGE.
link :https://developer.android.com/about/versions/13/behavior-changes-13#granular-media-permissions

Parfait answered 23/9, 2022 at 11:0 Comment(3)
can you please help me i have given all permission what you have mention here but still i am getting same errorHay
Thanks its worked it was my mistake i have not added conditionHay
How about reading PDF files?Rae
F
4

For Android 13, in manifest add

<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />

In Kotlin code:

private val PERMISSIONS =
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        arrayOf(
            Manifest.permission.READ_MEDIA_IMAGES,
            Manifest.permission.READ_MEDIA_VIDEO,
            Manifest.permission.READ_MEDIA_AUDIO
        )
    } else {
        arrayOf(
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA
        )
    }

    if (!HandleAndroidPermissions.hasPermissions(context, *PERMISSIONS)) {
        ActivityCompat.requestPermissions(
             requireActivity(),
             PERMISSIONS,
             101
        );
    } else {
        captureImage()
    }
Fouts answered 3/2, 2023 at 11:41 Comment(0)
B
2

I couldn't find a solution that fit my situation. I wanted to have permission for Android OSs 13 and lower to get the images from the gallery.

This is what I came up with:

My Manifest in 13(app needed to be used in min10):

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

Added in the main class of CONTAININGActivity, above the onCreate method:

    private static final int REQUEST_CODE_STORAGE_PERMISSION = 1;

Added in Class/Activity, outside the onCreate method:

 private static final String readExternalStorage;

    private static final String readMediaImages;

    public static String storage_permissions = readExternalStorage = Manifest.permission.READ_EXTERNAL_STORAGE;

    @RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
    public static String storage_permissions_33 = readMediaImages = Manifest.permission.READ_MEDIA_IMAGES;

    public static String[] permissions() {
        String p;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            p = storage_permissions_33;
        } else {
            p = storage_permissions;
        }
        return new String[]{p};
    }

I used this in the ActivityCompat for permissions:

ActivityCompat.requestPermissions(
                            CONTAININGActivity.this,
                            permissions(),
                            REQUEST_CODE_STORAGE_PERMISSION
                    );
Blear answered 13/7, 2023 at 0:7 Comment(0)
P
1
 private val mPermissions = if(Build.VERSION.SDK_INT >=33 ){
        arrayOf (
            Manifest.permission.READ_MEDIA_AUDIO,
            Manifest.permission.READ_MEDIA_IMAGES)
    }else{
        arrayOf (
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
        )
    }
Praline answered 28/9, 2023 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.