EasyPermissions Library: How to handle Runtime Permission in Android?
By: Doha Kash
Tags: Android App Development, Android Permissions, Android, AndroidDev, Android Libraries

I’ve been introduced To EasyPermissions Library for Android lately and I’ve liked the fact that it’s actually very easy to use, with few lines of code you are good to go. The library handles runtime permissions for you!

I’m going to create a simple app here that will show you how to use this library. The app consists of one screen/activity with a button and an imageView and when the user clicks on that button we want to open a photo picker and access the photos on his/her phone. Then when the user chooses a photo, we will display that photo in the imageView. And in order to do that we must ask the user for permission before accessing the photos.

So let’s start by adding the library dependency to your build.gradle file (Module:app).

dependencies {

// For developers using AndroidX in their applications

implementation 'pub.devrel:easypermissions:3.0.0'



// For developers using the Android Support Library

implementation 'pub.devrel:easypermissions:2.0.1'

}

This is the latest dependency version till the time of writing this article for developers who are using AndroidX and those who are still using the Android Support Library. However, you can check out the library yourself at this link:

After syncing your project gradle files, now we will start writing the code that will allow us to ask users for permission using EasyPermissions Library.

So create an activity with a single button and a single imageView as shown in the xml code below:

Now in the MainActivity, I’m going to implement the EasyPermissions.PermissionCallbacks interface. In my case, I’ll be using it in the activity but the same code will apply if you are using a fragment instead.

You will get an error because you’ll need to override the three methods of this interface which are onRequestPermissionsResult, onPermissionsGranted, and onPermissionsDenied. Just click alt + enter in Windows or option + enter in Mac on the error and a window will pop up that asks you to implement methods and click implement methods.

Here are the three methods that will show up now and we will focus on two of them. The first is onRequestPermissionsResult method and the second is the onPermissionsDenied and we will add the following code as shown below. However, I will leave onPermissionsGranted as it is.

Now when the user clicks on the button, a method called openPhotoPicker will be called as below. I will also declare a request code constant and I will name it for example READ_STORAGE_PERMISSION_REQUEST and give it a value of 123 for example. Also, I will declare another constant PICK_IMAGE_REQUEST that I will pass when I call startForActivityResult in the openPhotoPicker method.

In the openPhotoPicker method we will make sure to ask the user for permission. Just above the method add the annotation @AfterPermissionGranted() and put inside the parentheses the constant we just made. And inside the method declare a String variable called permission and chose which permission that you want the user to grant you. In this case, we want the Manifest.permission.READ_EXTERNAL_STORAGE.

As you can see it’s very easy to use, you create a permission variable and then use “if condition” to check the following if the EasyPermissions.hasPermissions(getActivity(), permission)) is true then go on with the method, otherwise ask the user for permission using EasyPermissions.requestPermissions and pass the context, the READ_STORAGE_PERMISSION_REQUEST , the permission variable ,and the message String you want to show the user in the permission dialog. In our case, the message is “Our App Requires a permission to access your storage”.

Now only when we have the user permission, we will open the photo picker and the user will be able to chose a photo. The photo that the user has chosen will return in the onActivityResult. When we get the requestCode of the PICK_IMAGE_REQUEST and the resultCode is RESULT_OK

We can get the chosen photo uri by calling the method data.getData();

You can do whatever you want with this chosen photo uri like displaying it in an imageView. Simply, you can use Glide library to display it as shown in the code. If you want to use Glide Library, add this dependency to your project:

dependencies {

implementation 'com.github.bumptech.glide:glide:4.11.0'

annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

}

Finally, don’t forget to add the read external storage permission to your AndroidManifest.xml file.

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

Here is the MainActivity Code after finishing all the steps:

And this is how the app will behave:

That’s all for now. Just give the EasyPermissions Library a try for handling your Android Permissions!