Kotlin, Android Studio... - Loading a JPG file into an imageview
File Permissions problem.
I have a small application which I load an image from storage
INITIALLY using Chooser, but on the subsequent calls directly using
the handle retrieved.
1. Initial load is using the file chooser - and this shows an image.
2. Subsequent loads are OK
3. Exiting the Emulator and then restarting (killing the permissions of course) - means I have to add permissions.
4. Try to add permissions for reading external storage fails.
What I have tried:
1. Added uses-permissions in the AndroidManifest.xml file as follows
<uses-permission android:name="android.permission.ACTION_OPEN_DOCUMENT"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2. Call to the chooser...
imgSelect.setOnClickListener{displayFileChooser()}
3. Function it called.
private fun displayFileChooser(){
val chooseFile = Intent(Intent.ACTION_OPEN_DOCUMENT)
chooseFile.addCategory(Intent.CATEGORY_OPENABLE)
chooseFile.type = "*/*"
startActivityForResult(chooseFile, 1)
}
This all works until I exit the emulator and restart....- killing the permissions.
Saved the Image URI and on load, simply set the image to the URI -
OBVIOUSLY, I will need permissions for this, thus on APPLICATION START, I have the following (in my onCreate() function).
val p = arrayOf("android.permission.ACTION_OPEN_DOCUMENT")
requestPermissions(p, 200)
and of course we override the onRequestPermissions....
<pre> override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
When I start the application after exiting the emulator - this onRequestPermissionsResult returns a -1 (permission denied)....
Thus, I cannot reload images unless I use the chooser again - which
really defeats the object here.
Would any kind person have any ideas how I can overcome this
heinous and annoying result and get permission approved.
Thanks