locked
Why won't my images display on Android? RRS feed

  • Question

  • User48786 posted

    I have a forms app that is working and looks great on iOS. The images for iOS are stored in a central assets folder and a file link is added to the iOS 'Resources' folder. So, trying to get the Android version tweaked and working I linked the files into the Android 'Resources/drawable' folder and set them to AndroidResource build action.

    I use a global style to set the background image for a 'BasePageStyle' in App.xaml in my core project. Since that had no effect in android I manually set the background image on a single content page to the background image for testing. When I build the android app I see in the output a line like:

    "Could not load image named: {0}: bg/MainBg.png"

    Not sure what is causing this.

    Two questions: 1) Should the global style images show or do they not work on Android? 2) Why do I get that line about the image not loading?

    Thanks, Jason

    Monday, November 2, 2015 10:33 PM

Answers

  • User142245 posted

    See if this works for the BackgroundImage property of ContentPage :

    <Setter Property="BackgroundImage">
            <OnPlatform
                      x:TypeArguments="string"
                      iOS="bg/MainBg.png"
                      Android="MainBg.png"
                      WinPhone="MainBg.png" />
    </Setter>
    

    If that doesnt work, try using FileImageSource or ImageSource as TypeArguments.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, November 3, 2015 7:39 AM

All replies

  • User48786 posted

    So, is my problem simply that they are in a subdirectory? Ive been poking around and was able to get it to work if I moved my image file to the drawable directory where it was previously in drawable/bg.

    If this is the case, can someone give an example of how to use OnPlatform inside a setter in Xaml for a global style?

    Tuesday, November 3, 2015 12:03 AM
  • User142245 posted

    See if this works for the BackgroundImage property of ContentPage :

    <Setter Property="BackgroundImage">
            <OnPlatform
                      x:TypeArguments="string"
                      iOS="bg/MainBg.png"
                      Android="MainBg.png"
                      WinPhone="MainBg.png" />
    </Setter>
    

    If that doesnt work, try using FileImageSource or ImageSource as TypeArguments.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, November 3, 2015 7:39 AM
  • User48786 posted

    @DushyantBangal Thanks, so close. It was actually <Setter Property="BackgroundImage"> <Setter.Value> <OnPlatform Android="MainBg.png" WinPhone="bg/MainBg.png" iOS="bg/MainBg.png" x:TypeArguments="x:String" /> </Setter.Value> </Setter>

    The other problem I had was that there were some images with a '-' (dash) in the filename. I didn't know this was a no no on android so it caused the Resource.Designer.cs to no regenerate. The error was a single line in the build output that read something like adb exited with error 1. Once I turned on detailed build messages in tools>options I was able to see more messages regarding the file names for each affected file. After I fixed the names the designer.cs file regenerated and all my images now show.

    Tuesday, November 3, 2015 1:06 PM
  • User142245 posted

    Glad to be of help. I just wrote it on Notepad so didnt know if it would work, missed the x:String part I also had the problem with '-' in filename. And the error it gives doesnt really explain what happened :no_mouth:

    Tuesday, November 3, 2015 1:16 PM