FFF
FFF

Reputation: 25

Mvvmcross binding file url to imageview

I try to bind imageview with local image file. In android, I can use setImageUrl to set image from a file outside resource folder. I read N+1 kitten example and try to use file url instead web url for my project. The layout of image view

<Mvx.MvxImageView
android:id="@+id/advisor_message_picture"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginLeft="8dp"
android:layout_alignParentRight="true"
local:MvxBind="ImageUrl MessageImage, Converter = Image" />

The converter use to join file name and file directory url. Android view file will set the FileDir

public class ImageConverter : MvxValueConverter<string, string>
{
    public static string FileDir;
    protected override string Convert(string value, Type targetType, object parameter, CultureInfo culture)
    {
        return FileDir + "/" + value;
    }
}

Update After the answer

I first copy or download to file to the Context.FilesDir.Path and check it with SetImageUrl, the image show up.

view.FindViewById<ImageView>(Resource.Id.advisor_message_picture).SetImageURI( new FileService(_context).CopyFileFromAssetsToStorage("image.png"));

Then I set the FileUrl of converter using same path and file name

ImageConverter.FileDir = FilesDir.Path;

In ViewModel

_messageImage = "image.png";
private string _messageImage;
public string MessageImage
{
     get { return _messageImage; }
     set { _messageImage = value; RaisePropertyChanged(() => MessageImage); }
}

It works now. The problem is I misunderstood the binding time of viewmodel

Upvotes: 1

Views: 4392

Answers (1)

Stuart
Stuart

Reputation: 66882

For Asset's you can bind using AssetImagePath using the ResourceLoader plugin. However, due to a sticky-fingers editing bug, this custom binding does currently need to added to your Setup - see https://github.com/slodge/MvvmCross/issues/372 for bug details

For files stored using the file plugin (which defaults to Context.FilesDir.Path - see https://github.com/slodge/MvvmCross/blob/v3/Plugins/Cirrious/File/Cirrious.MvvmCross.Plugins.File.Droid/MvxAndroidFileStore.cs#L39), you can use path directly.

For files stored in some custom FileDir determined within your app, you'll need to provide a path relative to Context.FilesDir.Path in order for the plugin to load it.

For further debugging, you could add breakpoints or trace to https://github.com/slodge/MvvmCross/blob/v3/Plugins/Cirrious/DownloadCache/Cirrious.MvvmCross.Plugins.DownloadCache.Droid/MvxAndroidLocalFileImageLoader.cs#L28 - or you could build and register your own IMvxLocalFileImageLoader<Bitmap> implementation that knows about your file paths.

Upvotes: 1

Related Questions