Forum Xamarin.Forms

windows phone and android images / resource file paths

Hi, in my shared project, i have images, but when i try to add these images to an image view or circle view it cannot locate them. i have added the image to the android project in the "resources/Drawable" folder but if i use the file paths:

image.source= "test.png" or
image.source= "Resources/Drawable/test.png" or
Image.soufrce= Drawable/test.png" or
image.source= "@test.png"

i have tried similar things in windows 8.1 and it cannot find them, i
it will never find the image. anyone know why?

Posts

  • devnldevnl NLUniversity ✭✭
    edited February 2016

    I use the top one of your options in my Xaml pages, so that should work in code behind too. You might want to check the build action, which for Android needs to be AndroidResource. I don't know if an image for the device density you're testing this on is provided in your case (Resource/Drawable-xxxhdpi e.g.). I'm also not quite sure if it will fall back to drawable if no image for that specific density is provided.

    For Windows I can't help you :(

  • Hi @devnl, yeah that worked a treat for getting it to work for android! Much appreciated! still cant get it to wrok for windows phone 8.1 tho, will play about and see if i can get it. if anyone else knows, feel free to post!

  • devnldevnl NLUniversity ✭✭
  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @christophergraham.1607 - in Xamarin forms the way images work is you need to put them in the native project for each platform.

    Android /Resources/Drawable
    iOS /Resources
    Windows / (just the root folder, yes its annoying but the only place it works)

    In WP8SL or WP8.1SL you put the largest image you need an it will down scale
    In WinRT or UWP you put in the images with .scale-140 etc to scale the images as needed
    In iOS you put @2x and @3x at the end of the image name for the larger scales
    In Android you put the appropriate images in the various drawable folders e.g. xxhdpi for each size you need.

    If you don't have the exact required image it always falls back to the default, which is just an images without @2x or scale-140 or not in the designated folder in the Resources/Drawable etc.

    With that you can reference the images anywhere in Xamarin Forms by just specifying its name

    Image.Source = "image.png";

    The platform takes care of the rest.

  • hey @AdamP, @devnl, thanks for the help, its much appreciated!! i got it working alright! and yeah, being in the root folder doesnt seem like a sensible place for them, but it works! thanks for all your help again.

  • MichaelRumplerMichaelRumpler ATMember ✭✭✭✭✭

    You can place your images in subfolders on iOS and Windows. Just include the path starting from the locations Adam listed.
    Unfortunately on Android you cannot use subfolders. The images have to be directly in the drawable* folders.
    So if you do use subfolders, then the image name must be different for each platform.

Sign In or Register to comment.