2

I am having trouble trying to show an image in a remote view with a contentUri. I have outlined the steps I took below. I believe I have followed the procedure correctly but still get a "Can't load widget" message. What am I doing wrong? Is the image saved to the correct storage area? Is the contentUri constructed correctly? Please help.

Blockquote

  1. Save the image into my file directory. I can see the image successfully saved in data/data/com.mydomain/files/myImage.jpg when I check using Android Studio's Device File Explorer.
val file = File(applicationContext.filesDir, "myImage.jpg")
try {
    FileOutputStream(file).use { out ->
        myBitmap?.compress(Bitmap.CompressFormat.JPEG, 90, out)
    }
} catch (e: IOException) {
    e.printStackTrace()
}
  1. Create a FileProvider in manifest and provider_paths.xml in res.xml
<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths" />
</provider>
<paths>
    <files-path name="name" path="." />
</paths>
  1. Create a content Uri using getUriForFile. This returns a content uri of: content://com.mydomain.provider/name/myImage.jpg
val file = File(getAppInstance().filesDir, "myImage.jpg")
val contentUri = FileProvider.getUriForFile(
    getAppInstance(),
    BuildConfig.APPLICATION_ID + ".provider",
    file
)
  1. I pass the content Uri into Image() composable.
Image(
    modifier = GlanceModifier.size(28.dp),
    provider = ImageProvider(contentUri),
    contentDescription = "Image"
)
  1. Result on run:

enter image description here

mars8
  • 770
  • 1
  • 11
  • 25
  • "Save the image into my file directory. I can see the image successfully saved in data/data/com.mydomain/files/myImage.jpg when I check using Android Studio's Device File Explorer." -- your code shows that you are using `cacheDir`, but everything else in your question only makes sense if you were using `filesDir`. "I believe I have followed the procedure correctly but still get a "Can't load widget" message" -- do you get any interesting Logcat messages? – CommonsWare May 16 '22 at 21:10
  • I have updated op to use `filesDir`, still getting same result though. I check the logcat but it does not show anything on placement of widget. I noticed there are number of directories I can save the file in e.g. `data\data\mydomain` or `data\user\0\mydomain` or `storage\emulated\0\Android\data\mydomain`. Am I saving the image in the right place? Is there anything else I could be doing wrong? Can I check the `contentUri` on something else apart from remoteViews? I want to check I have covered my bases before submitting a glance api bug report as you originally suggested. Many thanks – mars8 May 17 '22 at 04:47
  • "Am I saving the image in the right place?" -- so long as everything is consistent, and you do not get an exception or `null` response from `getUriForFile()`, you should be in reasonable shape. It is possible that Glance is not doing something that it needs to with respect to permissions, to allow the home screen's process to read your images. Or, perhaps Glance and `Uri` just do not get along (see https://stackoverflow.com/questions/71633437/glance-fails-to-load-images-using-the-coil and https://github.com/bumptech/glide/issues/4762). – CommonsWare May 17 '22 at 10:43
  • @CommonsWare As far as I can tell this looks like a bug. I have created a report on google's IssueTracker https://issuetracker.google.com/issues/233235354. This contains a sample project of the issue I encountered. If you are able to check the project that i am not doing anything silly here that would be much appreciated. Many thanks again for your help. – mars8 May 19 '22 at 20:23

0 Answers0