During making of your app, sometimes you may require to access external storage as a part to make your app more functional as your preliminary design suggests. It gets a bit straining if it's your first time or maybe you are just used to the previous approach which for some reason has gotten deprecated say.
Well you need not to worry about that no more, as i am going to give you a step by step tutorial on how to approach this in a more simplified and clean approach. Mind you, we are going to do this using Kotlin as our primary language of implementation. Let's get Started...
1. Declaration in your Android Manifest.
i Assume you already have your project created with kotlin as our back-end programming language. The first thing here you really wanna do is head to your manifest file and make you app aware of the list of permissions it's associated with.Simply add the following code in your android manifest.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"> The snippet above is very important and should not be forgotten as its one thing that makes all this process possible. You forget that and the request of permission wont be successful.
2. Check for permission status.
Before moving any further with this let me help you ask, why are we even checking for permission status while it's pretty obvious we don't have that access yet. Well at least for now we know that we don't, but for some reason someone could give us this access then revoke it later, next time they come back to the app, it may cause trouble. So it's a good conduct to always check it. Use the following code to do this in your Activity.kt file
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
// do some stuff here
}else{
softoniaLauncher.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
What's happening above there is that we checked if our app is already granted with the access to read external storage or not, well during the initial launching of the app, it makes more sense and it's very obvious that our true portion of the conditional statement won't be executed. This brings us to the second portion of our condition, the else part.
3. What exactly is softoniaLauncher above there?
Well, to that i tell you worry no more, it's definition is right below
private var softoniaLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()){ isPermitted ->
if (isPermitted){
// Do stuff here
}
} SoftoniaLauncher is simply a kotlin variable you have got to define somewhere below which which makes use of the method registerForActivityResult. What it does it to help lift the intent we commonly see which tells us to allow this app to access photos and stuff and provides us with the Allow or Deny functionalities. If you are careful observing the code above, you will realize it also waits for the Allowance or Denial then returns a Boolean to indicate the permission was granted or otherwise. So you need to make a smart use of the boolean returned to make decisions as i did above there. Pretty smart Huh ?
Conclusion
I am very proud to declare, that's all you need to request permission for reading external storage of a mobile device using your app. It's a run time process meaning that the whole thing is going to be possible during active sessions of the app, it's pretty simple and works 100%.
Stay awesome
#Sam
0 Comments