Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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
Posted
Comments
Richard MacCutchan 30-Apr-23 4:19am    
How do you expect to open the image file without using the file chooser? What reference are you using to access the file in the second attempt?
Member 10488837 30-Apr-23 5:33am    
Thanks for your time sir.
Using the URI I previously stored - simply set the imaged to the saved URI.
"imgNode.setImageURI(currentURI)"
When I do this I get a permissions Exception of permission denied.
As I am getting permission denied, I thought I didn't need to actually open
the file as it was permission problem not open problem.
Richard MacCutchan 30-Apr-23 6:39am    
What is the actual content of currentURI at that point?
Member 10488837 30-Apr-23 7:48am    
content://com.android.externalstorage.documents/document/primary%3APictures%2FSierra%20%26%20Wolfy.jpg

Richard MacCutchan 30-Apr-23 7:50am    
Try adding the permissions that you originally used to open the file.

1 solution

From the Android documentation:-

Persist permissions
When your app opens a file for reading or writing, the system gives your app a URI permission grant for that file, which lasts until the users device restarts. Suppose, however, that your app is an image-editing app, and you want users to be able to access the 5 images that they most recently edited, directly from your app. If the users device has restarted, you would have to send the user back to the system picker to find the files.
To preserve access to files across device restarts and create a better user experience, your app can "take" the persistable URI permission grant that the system offers.

The following code is inserted just before the currImg.SetImageURI(currentURI)

val rPermPersist = Intent.FLAG_GRANT_READ_URI_PERMISSION
this.context!!.contentResolver.takePersistableUriPermission(currentURI, rPermPersist)
currImg.setImageURI(currentURI)


This allows me to restart the emulator and then have persistent permission to that particular file.
 
Share this answer
 
Comments
Richard MacCutchan 1-May-23 3:10am    
No idea why this was downvoted, I have added a 5. And well done for finding the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900