How to set image (any file size) of ImageView when creating (home screen) widget in Android (API +24) in Kotlin

120 Views Asked by At

I have a problem, I need to create a widget in Kotlin which has to get images from system gallery and display them in ImageView (random pick).

I successfully made it work in MainActivity (which mustn't exist) and in widget's configure activity (which is optional and shouldn't exist either). But when I try to use RemoteViews.setImageViewUri() (instead of using the same method on ImageView or ImageSwitcher directly) with Uri that I took from querying MediaStore.Images.Media.EXTERNAL_CONTENT_URI I get an error:

android.os.FileUriExposedException: file:///storage/0000-0000/path/to/file.png exposed beyond app through RemoteViews.setUri()

As I understood from countless hours of trying to achieve the goal, the activity which executes this method is the home screen activity, and it has no permissions for reading arbitrary files. But my application (widget) has them (<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />). And there is no Intent to which you can give any permission flag. I tried using custom ContentProvider and FileProvider but it always says:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/0000-0000/path/to/file.png

no matter which paths I include in XML file.

I want to know some sort of MWE of how to approach this problem, because there are practically 0 examples on the Internet. (I saw bitmap manipulation which is only possible for small images, but also some questions that have the same problem when trying to pass Uri to RemoteViews.setImageViewUri() raw/naked or in any kind of FileProvider wrapper and neither have worked for me so far).

The project template that I've started with (in Android Studio) is No Activity + File > New > Widget > App widget (Configuration Screen checkbox is checked).

P.S. My code base looks very messy and very big right now (after all the trials and errors). This is why I just need some guidance, as I don't have anything in particular that is working. All the key points I (think I) already told.

0

There are 0 best solutions below