Forum Xamarin.Forms

How to DownLoad Network Image And Save Image to mobile storage

DragonHuaDragonHua USMember ✭✭

How to DownLoad Network Image And Save Image to mobile storage

Best Answer

Answers

  • RyanDixonRyanDixon USMember ✭✭✭

    Many ways to go about this...

    Most Effort) Manually use System.Web to download and then write dependencies for each platform to save them.

    Medium Effort) Manually use System.Web to download and then use PCLStorage to bypass the need to write dependencies.

    Lazy Mans Route) Use Akavache and its extension method LoadImageFromUrl, then either use it to save to SQLite or use PCLStorage to save to disk.

    Full props to Paul Betts and Daniel Plaisted for their plugins!
    https://github.com/dsplaisted/PCLStorage
    https://github.com/akavache/Akavache

  • Sujata_BSujata_B Member ✭✭

    @VenkataSivaprasadReddyPulagam said:
    by using WebClient in Android and Ios using Dependencies

    in pcl

    write a button click event
    and also create a dependency service

    in android and in ios

    using System.Net;
    using System.IO;
    using System.Text;
    
    [assembly : Dependency(typeof(ShortcutGenerateService))]
    namespace Downloadfile.Droid
    {
        public class DownloadService : IDownloadService
        {
          public async void DownloadImage()
          {
          var webClient = new WebClient();
              webClient.DownloadDataCompleted += (s, e) => {
        var bytes = e.Result; // get the downloaded data
        string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        string localFilename = "downloaded.png";
        string localPath = Path.Combine (documentsPath, localFilename);
        File.WriteAllBytes (localPath, bytes); // writes to local storage
    };
          var url = new Uri("https://www.xamarin.com/content/images/pages/branding/assets/xamagon.png");
          webClient.DownloadDataAsync(url);   
              
              
          }
        }
    }
    

    What code must be present in IDownloadService interface? Can you help me....

Sign In or Register to comment.