I updated my android project from compileSdkVersion 31 to compileSdkVersion 33. I have the next code to launch the permissions that I need in the app, but it is not working
import android.Manifest
import android.app.Activity
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import android.os.Build
class Permissions(private val activity: Activity, private val permissionList: List<String>, val code: Int) {
fun checkPermissions() {
if (permissionsGranted() != PackageManager.PERMISSION_GRANTED) {
requestPermissions()
}
}
private fun permissionsGranted(): Int {
var counter = 0
for (permission in permissionList) {
counter = ContextCompat.checkSelfPermission(activity, permission)
}
return counter
}
private fun deniedPermission(): String {
for (permission in permissionList) {
if (ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_DENIED) return permission
}
return ""
}
private fun requestPermissions() {
val permission = deniedPermission()
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
} else {
ActivityCompat.requestPermissions(activity, permissionList.toTypedArray(), code)
}
}
fun checkPermission(manifest: String): Boolean {
val result = ContextCompat.checkSelfPermission(activity, manifest)
return result == PackageManager.PERMISSION_GRANTED
}
fun checkPermissionActivity(state: Boolean, urlPath: String, kind : String){
val showRationale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
this.activity.shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE) && this.activity.shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)
} else {
true
}
if (!showRationale) {
return
} else {
return
}
}
fun getAllPermissions(): List<String> {
val granted = ArrayList<String>()
val pi = activity.packageManager.getPackageInfo(activity.packageName, PackageManager.GET_PERMISSIONS)
for (i in pi.requestedPermissions.indices) {
if (pi.requestedPermissionsFlags[i] and PackageInfo.REQUESTED_PERMISSION_GRANTED != 0) {
granted.add(pi.requestedPermissions[i])
}
}
return granted
}
}
The manifest is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.co.retrofit.app">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:name=".feature.RetrofitApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity
android:exported="true"
android:name=".feature.view.activities.Maintenance" />
<activity android:name=".feature.view.activities.SplashActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".feature.view.activities.MainActivity" android:exported="true"/>
</application>
</manifest>
I call the function when I click in one button
@Suppress("UNUSED_PARAMETER")
private fun addAlbum(view: View){
val permissions = Permissions(this,
arrayListOf(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE),
23)
permissions.checkPermissions()
}
or
@Suppress("UNUSED_PARAMETER")
private fun addAlbum(view: View){
val permissions = Permissions(this,
arrayListOf(
Manifest.permission.CAMERA),
23)
permissions.checkPermissions()
}
If I rollback the updated, it is showing the permission that I need, but I need the compile in 33
Now if I only update the compile SDK, it is not showing the popup
CodePudding user response:
By targeting Android 13, you need to declare in our manifest what kind of media you exactly want to read. You can not request READ_EXTERNAL_STORAGE
in manifest directly.
Suppose you want an audio permission then READ_MEDIA_AUDIO
, for video permission READ_MEDIA_VIDEO
, for images READ_MEDIA_IMAGES
etc.
Also if you are migrating to Android 13 then this article can help a lot.
CodePudding user response:
With Android 13, Google Introduced Granular Permissions for Storage meaning you now will have to specify which type of media you want to use in your app.
There are 3 types of permission introduced :
READ_MEDIA_AUDIO
,
READ_MEDIA_VIDEO
and
READ_MEDIA_IMAGES
.
U can no longer request READ_EXTERNAL_STORAGE
permission on Android 13, and by no longer I mean no one has been able to identify why. Once u compile for sdk 33, it just stops asking on runtime and removes the permission from the App Settings as well.
Refer to this for more info : https://developer.android.com/about/versions/13/behavior-changes-13#granular-media-permissions