From 42e6e5fa884087446b0e2ab1eebb087c9422d780 Mon Sep 17 00:00:00 2001 From: Dhaval Patel Date: Sun, 9 May 2021 21:33:52 +0530 Subject: [PATCH] takePersistableUriPermission from gallery image --- .../imagepicker/provider/GalleryProvider.kt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/GalleryProvider.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/GalleryProvider.kt index b794b4c7..92a0a8e7 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/GalleryProvider.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/GalleryProvider.kt @@ -4,6 +4,7 @@ import android.Manifest import android.app.Activity import android.content.Context import android.content.Intent +import android.net.Uri import android.os.Bundle import androidx.core.app.ActivityCompat.requestPermissions import com.github.dhaval2404.imagepicker.ImagePicker @@ -152,9 +153,19 @@ class GalleryProvider(activity: ImagePickerActivity) : private fun handleResult(data: Intent?) { val uri = data?.data if (uri != null) { + takePersistableUriPermission(uri) activity.setImage(uri) } else { setError(R.string.error_failed_pick_gallery_image) } } + + /** + * Take a persistable URI permission grant that has been offered. Once + * taken, the permission grant will be remembered across device reboots. + */ + private fun takePersistableUriPermission(uri: Uri) { + contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION) + } + }