0

I am working on an app where I want users to get images from web service but don't want them to be stored locally in localdb or local files. Is there a way to do it?

I am using Xamarin forms.But if it can be done natively it can also be done through xamarin forms.

TheDeveloper
  • 1,127
  • 1
  • 18
  • 55

2 Answers2

1

You can always download the image as a file using a webclient and then an StreamImageSource.

First, you need an interface for a dependency service (I prefer to use WebClient than any third party library so I use these services, feel free to use anything else if you like it, just skip the service part) created on the Forms project:

public interface IDownloader
{
    byte[] Download(string Url);
}

Then, you need a dependency service on the Android project:

[assembly: Dependency ( typeof (--yournamespace--.Downloader))]
public class Downloader : IDownloader
{
    public byte[] Download(string Url)
    {
        //This code is synchronous, I would recommend to do it asynchronously

        WebClient wc = new WebClient();
        return wc.DownloadData(Url);
    }
}

And then you can download the image and set it as source:

//I assume there exist an image control called img.

var dl = DependencyService.Get<IDownloader> ();
byte[] data = dl.Download(--url to the image--);
var ms = new MemoryStream(data);
img.Source = new StreamImageSource{ Source = (t) => ms };
Gusman
  • 14,905
  • 2
  • 34
  • 50
  • If we follow this procedure and set image source as memory stream the images are not saved anywhere in the device correct? – TheDeveloper Oct 19 '16 at 17:58
  • Yes, Xamarin will just use the Stream to create the Android Bitmap and that's all done in memory. – Gusman Oct 19 '16 at 17:59
  • Does the same apply to iOS ? – TheDeveloper Oct 19 '16 at 18:00
  • I think so but not 100% sure if iOS (the OS itself) can cache the images. – Gusman Oct 19 '16 at 18:01
  • I will cross check that. But thank you. Your answer was a great help. I am trying to post another question regrading disabling the screenshot feature on iOS, i know it can be done on android but can it also be done on iOS? – TheDeveloper Oct 19 '16 at 18:03
  • No it can't, take a look at this: http://stackoverflow.com/questions/18680028/prevent-screen-capture-in-an-ios-app – Gusman Oct 19 '16 at 18:04
  • Also, remember that in Android if the user has some remote control app then he can take any screenshot he wants even if it's disabled. – Gusman Oct 19 '16 at 18:05
0

You don't mention if you're online or not - if you're online you can just use a URL in the XAML for Xamarin forms, eg:

 <Image HeightRequest="50" WidthRequest="50" HorizontalOptions="Center" VerticalOptions="Center" Source="{Binding UserAvatarURL}"/>

And Forms will handle it.

Russell Young
  • 2,033
  • 1
  • 14
  • 12
  • if we pass the url in 'UserAvatarURL' it will be displayed to the user but it will not be stores anywhere locally or in any temp files ? – TheDeveloper Oct 19 '16 at 17:12
  • In the case above, it's a databound field, so the actual text will be replaced by a real URL in my view model, eg. https://example.com/image.png - if you want to replace the binding to try it, use something like this (obviously replace the url with one that's real): – Russell Young Oct 19 '16 at 17:25
  • It will be possibly stored on a temp file, but that will be erased by the OS, the other option is to download the file to a memory array and set the imagesource to a MemoryStream using that array – Gusman Oct 19 '16 at 17:25
  • @Gusman yes - I was thinking the op's issue was around having to do the saving to local work themselves, but you're right, I may be making a false assumption, and the images are cached locally still, they do seem very temporary though. – Russell Young Oct 19 '16 at 17:27
  • @RussellYoung Yes, they will be temporary, but if the image contains sensitive data then that can be a security hole. But anyway, I would never, ever, store sensitive data in any case, the only legit case for this I can think is for scans of documents or images of medical data. – Gusman Oct 19 '16 at 17:31
  • @RussellYoung When you say temporary how temporary are we talking and is there any way user can save it before they are cleaned by os ? – TheDeveloper Oct 19 '16 at 17:33
  • I don't believe the user can get to the image to save it to another store, but I may be wrong - I'm guessing if they really wanted to they can use the device's screenshot capability anyway? – Russell Young Oct 19 '16 at 17:43
  • @Gusman Can you please elaborate on how I can download file to memory array and set to image source ? May be small code if you can ? – TheDeveloper Oct 19 '16 at 17:44
  • Let me write an answer – Gusman Oct 19 '16 at 17:44
  • @RussellYoung on android you can set flag to disable screenshot feature not sure about iOS – TheDeveloper Oct 19 '16 at 17:44
  • @Azhar- wait for Gusman's write up of image.Source = ImageSource.FromStream() I think that will suit better – Russell Young Oct 19 '16 at 17:47