Matthew The Terrible
Matthew The Terrible

Reputation: 1643

How to store image uri from gallery in database for loading later in Xamarin.Android?

I want to let a user load an image from their gallery:

var imageIntent = new Intent();
            imageIntent.SetType("image/*");
            imageIntent.SetAction(Intent.ActionGetContent);
            StartActivityForResult(Intent.CreateChooser(imageIntent, "Select photo"), REQUESTCODE);

This works fine: I can use the image in that activity. But I want to store the URI in the database so the image is associated with the object. I use:

_recipeImage.SetImageURI(data.Data);
model.ImageUrl = data.Data.ToString();

_recipeImage is just an image view I'm using to see if I actually got the image correctly for now.

When I save my model to the database I get a string that looks something like:

content://com.android.providers.downloads.documents/document/22

When the user opens this item later the image should load so I parse the string to a Uri. I try and set the image URI as I did before, but no image gets shown.

var uri = Android.Net.Uri.Parse(imageUrl);
imageView.SetImageURI(uri);

Is there a better/working way to do this?

Upvotes: 2

Views: 392

Answers (1)

Grace Feng
Grace Feng

Reputation: 16652

I think you want to load image directly use URL, it's easier to use some third-party lib, for example Picasso.

Since it's a java lib, you can use Binding Library to use it in Xamarin.

After adding this lib into your project, you can simply code like this:

Picasso.With(this).Load(imageUrl).Into(imageView);

Upvotes: 1

Related Questions