how to store URI of a file from gallery into local DB and access it (in kotlin preferably)

835 Views Asked by At

I have implemented ActvityResults API to get an image from gallery (or) directly from camera.

for taking a image from camera it's simple

  1. create a file obj in your app's local directory.

  2. create a URI for file obj

  3. using ActivityResults API, send this uri in the ActivityResultLauncher (This will automatically write the image that you capture to the URI)

  4. just store the URI as String in DB and use it (no permission/access issues)

but when selecting an image using ActivityResult API contracts from Storage,

It returns a URI.

If I store this URI in my DB... I cant access it because the image file isn't local to my app and therefore I don't have permission to access it

so I'm looking for a method to copy this URI's content into a File obj (which will be created in the app's local directory.

So that I can store the file's URI as string in my DB

P.S [ many questions answering this are at least 7 years old and are in JAVA (they use the cursor API, so if you have a solution much simpler than that please share your knowledge & expertise ) ]

0

There are 0 best solutions below