1

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 ) ]

SIMPLEGUY
  • 163
  • 7
  • 1
    Are you using the `GetContent` or `OpenDocument` contract to select an image? – ianhanniballake Dec 27 '21 at 16:30
  • Im using GetContent() – SIMPLEGUY Dec 27 '21 at 16:32
  • 2
    You could switch to `OpenDocument`, then use `takePersistableUriPermission()` on a `ContentResolver`, then persist the `Uri` as you are doing elsewhere. See https://commonsware.com/blog/2020/08/08/uri-access-lifetime-still-shorter-than-you-might-think.html for more about content access rights. "I'm looking for a method to copy this URI's content into a File obj" -- call `openInputStream()` on a `ContentResolver`, then copy the bytes using standard Java I/O. – CommonsWare Dec 27 '21 at 16:35

0 Answers0