Ad Code

Android 13 Read Media Permission integration documentation in Android Studio project | How to check and request Image, Video and Audio permissions in Android TIRAMISU

Hellow Developers, in this Article I have given an example that how can we Integrate Read external Storage and Read Media Permission in Android 13 and below. In android 13 READ_EXTERNAL_STORAGE permission is removed and introduced READ_MEDIA permission for getting media content like Images, Videos and Audio. So, in this project we will check device android version and request the permission according to the Android Version.

Android 13 is the latest version of Google's mobile operating system, set to be released in the latter half of 2023. It is expected to bring several improvements and new features to Android devices.


Android 13 Read Media Permission integration.


Android 13 read media permission



Here is the permissions you need to add in the AndroidManifest.xml file. Just add the permission which you need in you project. For example, if you need Audio files then you just add READ_MEDIA_IMAGE permission. But you need to add READ_EXTERNAL_STORAGE permission for below Android 13.

Permissions
<!--For android 13 or later-->
    <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
    <uses-permission android:name="android.permission.READ_MEDIA_AUDIO"/>

     <!--For below android 13-->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Here is the permission request code

 int PERMISSION_REQUEST_CODE = 1001;

Now check the permission is granted or not. It will return the value true or false.

Check Permission
    private boolean checkReadStoragePermission() {

        if (SDK_INT >= Build.VERSION_CODES.TIRAMISU) {

            int result = ContextCompat.checkSelfPermission(this, READ_MEDIA_AUDIO);
            int result1 = ContextCompat.checkSelfPermission(this, READ_MEDIA_IMAGES);
            int result2 = ContextCompat.checkSelfPermission(this, READ_MEDIA_VIDEO);
            return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED && result2 == PackageManager.PERMISSION_GRANTED;

        } else {

            int result = ContextCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE);
            return result == PackageManager.PERMISSION_GRANTED;

        }
    }

Now check the Android Version of the device, if it Android 13 or more then call READ_MEDIA permission request, otherwise you can call READ_EXTERNAL_STORAGE permission request.

Request Permission
  private void requestReadStoragePermission() {

        if (SDK_INT >= Build.VERSION_CODES.TIRAMISU) {

            ActivityCompat.requestPermissions(this, new String[]{READ_MEDIA_AUDIO, READ_MEDIA_IMAGES, READ_MEDIA_VIDEO}, PERMISSION_REQUEST_CODE);

        } else {

            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);

        }


    }

The request permission result will call after user Accept or Deny the requested permission.

Permission Result
 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        if(requestCode == PERMISSION_REQUEST_CODE) {

            if(checkReadStoragePermission()) {
                Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Permission not granted", Toast.LENGTH_SHORT).show();
            }

        }


        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

Here is the uses. You can paste this code in OnCreate method or OnClick function. Now, check if user already granted the permission or not, If user does not granted the permission then call requestPermission function, If user granted then load the media which you want.

Uses
      if(!checkReadStoragePermission()) {

           requestReadStoragePermission();

        }

Android 13, the latest version of Google's mobile operating system, includes some significant changes to storage permissions. These changes are aimed at improving user privacy and security, and are designed to give users more control over how apps access their data.

One of the most notable changes is the introduction of scoped storage, which limits the access that apps have to a user's storage. In previous versions of Android, apps could access any file on a user's device as long as they were granted permission to do so. With scoped storage, however, apps can only access files that are within their own "sandbox" area, which is designed to prevent unauthorized access to a user's data.

Another change in Android 13 is the introduction of a new permission called "access media." This permission is required for apps that need to access files that are outside of their own sandbox area, such as files that are stored in a user's Downloads or Pictures folders. This permission is granted on a per-file basis, so users can choose to grant or deny access to specific files based on their preferences.

In addition to these changes, Android 13 also includes improvements to the way that apps request storage permissions. Apps are now required to provide more detailed information about why they need access to a user's storage, and users can now choose to grant or deny access on a case-by-case basis.

Overall, the changes to storage permissions in Android 13 are designed to improve user privacy and security, while also giving users more control over how apps access their data. While these changes may require some adjustments for app developers, they ultimately benefit both users and developers by creating a more secure and transparent app ecosystem.

Thank you for visiting our website. If you did not understand or facing error then you can contact us or comment below. We will try to replay your comment as soon as possible.


Post a Comment

0 Comments

Ad Code