Click here to Skip to main content
15,909,939 members

Comments by Member 10488837 (Top 7 by date)

Member 10488837 30-Apr-23 14:55pm View    
Sorted: - here's the thing....

From the Kotlin Android Documentation -
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 user's 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 user's device has restarted, you'd 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.

Thus, before I set the image URI -> currImg.SetImageURI(currentURI)

I have inserted the following... Here you can see I have the SetImage as the last line.

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

This has thus far worked over emulator restarts..

My grateful thanks for your excellent assistance which
gave me the inspiration and ideas upon where to look and
without this, I would have taken an eternity to solve.


Member 10488837 30-Apr-23 12:31pm View    
Heyy, no worries,
Thanks for your ideas -
it has made me think in different directions
and that's teamwork - I am very grateful and
will post here if I manage to find a solution

It's very kind of you to take the time
Member 10488837 30-Apr-23 12:16pm View    
Thanks Sir, tried a couple of things...

1. Added CATEGORY_OPENABLE to the permissions so it looks like this...
var p = arrayOf("android.permission.ACTION_OPEN_DOCUMENT", "android.permission.CATEGORY_OPENABLE")

Also - I noticed that the request code for the chooser was 1 and
in the open I used 200 - so I changed this to 1 too to match the chooser
requestPermissions(p, 1)


Interestingly, now I get an array of int from the permissonsresult
Which is [-1], [-1] - both seem to fail!!

Thanks for the good leads thus far though
Member 10488837 30-Apr-23 12:04pm View    
Agreed, thanks -
I thought that was what I was doing - I don't use the chooser second time,
Just simply call for permission using the same intent
The call for permissions is this
"val p = arrayOf("android.permission.ACTION_OPEN_DOCUMENT")"
"requestPermissions(p, 200)"

Then using the stored URI, I simply put that in my setting of the Image URI
like this -> "imgNode.setImageURI(currentURI)"

I am sorry perhaps I didn't make that clear.
Member 10488837 30-Apr-23 7:56am View    
Sorry sir, don't understand...
I used the chooser - and permission was set to ACTION_OPEN_DOCUMENT
Obviously that's the permission I need but I thought I set that in
these 2 statements.
"val p = arrayOf("android.permission.ACTION_OPEN_DOCUMENT")"
"requestPermissions(p, 200)"

Is that not correct? - should I do more.

Thanks so much for your time

EDIT: -
Just to clarify

When I do the requestPermissions(p,200) - this obviously goes through
the override "onRequestPermissionsResult()" - and in there I am getting
the PermissionDenied... - This is in the CreateView() of the parent form.
I will try this in the current form.

Further Edit: -
Utilising the requestPermissions call in the current form still has no change,
I am getting the following Exception...
java.lang.SecurityException: Permission Denial: opening provider com.android.externalstorage.ExternalStorageProvider from ProcessRecord{cdb11de 6067:com.example.missioncontroller/u0a153} (pid=6067, uid=10153) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs

In Addition, following the call - it goes through the "onRequestPermissionsResult" override.
This gives a -1 in the grantResults field

Just for completeness, here's the function I originally used to call the chooser -
private fun displayFileChooser(){
val chooseFile = Intent(Intent.ACTION_OPEN_DOCUMENT)
chooseFile.addCategory(Intent.CATEGORY_OPENABLE)
chooseFile.type = "*/*"
startActivityForResult(chooseFile, 1)
}
And of course - the onActivityResult

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 1 && resultCode == Activity.RESULT_OK){
val content = data!!.data
val src = content!!.path
currImg.setImageURI(content)
currentURI = content
currChk.isChecked = true
checkDataChanged()
}
}