1

I am new to using Uri's to locate files on the emulators storage in android studio.

I have a png file saved in the android emulators data/data/com.domain.appname/files directory. I would like to create the uri for this file using android's Uri.Builder() function. I want to make sure the uri is consctucted correctly, rather than hardocding the path myself, hence using the builder api.

How would i go about doing this? The link the to builder api is https://developer.android.com/reference/kotlin/android/net/Uri.Builder. I have started creating the uri myself below but not sure what to do next.

Context: I am using googles Glance API to show the image in an android homescreen widget. The api asks for a URI parameter.


val uri = Uri.Builder().scheme(ContentResolver.SCHEME_FILE)
          ./*insert rest of Uri builder code*/

mars8
  • 770
  • 1
  • 11
  • 25
  • It would seem to be easier to build a `File` object pointing to your file (using `getFilesDir()` on `Context`), then using `Uri.forFile()` to get a `Uri`. Note that a `Uri` with a `file` scheme cannot be passed to other apps (e.g., via an `Intent`); for that, you should use `FileProvider`. – CommonsWare May 09 '22 at 18:30
  • If you have a path like /data/data/com.domain.appname/files/image.png then the uri would be file:///data/data/com.domain.appname/files/image.png. I have no idea why you would need a builder for that or whatever. – blackapps May 09 '22 at 18:51
  • thanks @CommonsWare, i have tried something similar with `getUriForFile()` in https://stackoverflow.com/q/71938442/15597975 but still no luck in showing image. If you are able to look at this question and provide any feedback then that would be greatly appreciated. Seems like a bug to me where new Glance API doesn't work with URI. – mars8 May 09 '22 at 18:56
  • Since app widgets are rendered by other processes, you would need to use `FileProvider`. If that is not working, create a project that reproduces the problem and file a bug report. I do not see a bug report related to this right now. – CommonsWare May 09 '22 at 19:19
  • @CommonsWare I have tried to use `FileProvider` and it is still not showing the image in the widget. I am new to using File Provider and would be grateful if you could take a look and let me know if I am doing something wrong https://stackoverflow.com/q/72265361/15597975 thanks – mars8 May 16 '22 at 20:44

0 Answers0