Android 12 和 13 中的权限问题

问题描述 投票:0回答:1

以前我的应用程序的目标sdk是31,我的设备是Android 12,我授予相机和存储权限。即使授予READ_EXTERNAL_STORAGE权限后,将targetsdk更新到33后也不会打开手机图库

通过检查运行设备的设备sdk来处理代码。

android kotlin sdk local-storage android-permissions
1个回答
0
投票
    If your Android app was previously targeting SDK 31 and was able to access the phone's gallery with the READ_EXTERNAL_STORAGE permission, but it stopped working after updating the target SDK to 33, it's likely because of the new storage permission changes introduced in Android 11 (SDK 30) and Android 12 (SDK 31).
    
    Starting from Android 11 (SDK 30), apps can no longer access the entire external storage with the READ_EXTERNAL_STORAGE permission. Instead, apps must use the Storage Access Framework (SAF) to request access to specific files or directories. This is a security enhancement to protect user data and privacy.
    
    
    If your Android app was previously targeting SDK 31 and was able to access the phone's gallery with the READ_EXTERNAL_STORAGE permission, but it stopped working after updating the target SDK to 33, it's likely because of the new storage permission changes introduced in Android 11 (SDK 30) and Android 12 (SDK 31).
    
    Starting from Android 11 (SDK 30), apps can no longer access the entire external storage with the READ_EXTERNAL_STORAGE permission. Instead, apps must use the Storage Access Framework (SAF) to request access to specific files or directories. This is a security enhancement to protect user data and privacy.
    
    To fix this issue and make your app compatible with Android 12 (SDK 33) and above, you'll need to update your code to use the Storage Access Framework. Here's a step-by-step guide on how to do this in Kotlin:
    
     1. **Open the AndroidManifest.xml file and make sure you have the**
        READ_EXTERNAL_STORAGE permission declared:
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
    **In your activity or fragment where you want to access the gallery, you should use an Intent to open the system file picker using the Storage Access Framework.**
  

     // Initialize the permission request launcher
            requestPermissionLauncher =
                registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
                    if (permissions[android.Manifest.permission.READ_EXTERNAL_STORAGE] == true) {
                        // Permission granted, you can now open the file picker
                        openFilePicker()
                    } else {
                        // Permission denied, handle accordingly
                        // You might want to show a message to the user explaining why you need this permission
                    }
                }
    
            // Check if permission is granted, and request it if necessary
            if (!hasStoragePermission()) {
                requestStoragePermission()
            } else {
                // Permission is already granted, you can open the file picker
    
   // Make sure to adapt the code to your specific use case and handle the selected file according to your app's requirements. This should allow your app to access the gallery on Android 12 (SDK 33) and above while complying with the new storage permission rules
                openFilePicker()
            }
        }
    
        private fun hasStoragePermission(): Boolean {
            return checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
        }
    
        private fun requestStoragePermission() {
            requestPermissionLauncher.launch(
                arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE)
            )
        }
    
        private fun openFilePicker() {
            val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
            intent.addCategory(Intent.CATEGORY_OPENABLE)
            intent.type = "image/*" // You can change the MIME type to match the type of files you want to access (e.g., "image/*", "audio/*", "video/*", etc.)
            startActivityForResult(intent, REQUEST_CODE_OPEN_DOCUMENT)
        }
    
        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
    
            if (requestCode == REQUEST_CODE_OPEN_DOCUMENT && resultCode == Activity.RESULT_OK) {
                // Handle the selected file here
                val selectedFileUri = data?.data
                if (selectedFileUri != null) {
                    // Do something with the selected file URI
                }
            }
        }
    
        companion object {
            private const val REQUEST_CODE_OPEN_DOCUMENT = 123
        }
© www.soinside.com 2019 - 2024. All rights reserved.