Forum Xamarin.Forms

How can I save a video from an url, to internal storage?

I've tried this code, but I can't play the video

string pathToNewFolder = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, folder);
        Directory.CreateDirectory(pathToNewFolder);

        try
        {
            WebClient webClient = new WebClient();
            webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
            string pathToNewFile = System.IO.Path.Combine(pathToNewFolder, System.IO.Path.GetFileName(url) + ".mp4");
            webClient.DownloadFileAsync(new System.Uri(url), pathToNewFile);
        }
        catch (Exception ex)
        {
            if (OnFileDownloaded != null)
            OnFileDownloaded.Invoke(this, new DownloadEventArgs(null,null,null,null,0));
        }
        return pathToNewFolder;

Best Answer

  • danielparedes97danielparedes97 Member ✭✭✭
    Accepted Answer
    public string DownloadFile(string url)
            {
                var source = Android.Net.Uri.Parse(url);
                var request = new DownloadManager.Request(source);
                request.AllowScanningByMediaScanner();
                request.SetAllowedOverRoaming(false);
                request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
                request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, source.LastPathSegment);
                var manager = (DownloadManager)Xamarin.Forms.Forms.Context.ApplicationContext.GetSystemService(Android.Content.Context.DownloadService);
                manager.Enqueue(request);
    
    
                string name = "";
                name = System.IO.Path.GetFileName(url);
    
                return name;
            }
    

Answers

  • danielparedes97danielparedes97 Member ✭✭✭
    Accepted Answer
    public string DownloadFile(string url)
            {
                var source = Android.Net.Uri.Parse(url);
                var request = new DownloadManager.Request(source);
                request.AllowScanningByMediaScanner();
                request.SetAllowedOverRoaming(false);
                request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
                request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, source.LastPathSegment);
                var manager = (DownloadManager)Xamarin.Forms.Forms.Context.ApplicationContext.GetSystemService(Android.Content.Context.DownloadService);
                manager.Enqueue(request);
    
    
                string name = "";
                name = System.IO.Path.GetFileName(url);
    
                return name;
            }
    
  • ColeXColeX Member, Xamarin Team Xamurai
    edited August 2019
  • JeanPiereJeanPiere Member ✭✭

    danielparedes97> @danielparedes97 said:

    public string DownloadFile(string url)
            {
                var source = Android.Net.Uri.Parse(url);
                var request = new DownloadManager.Request(source);
                request.AllowScanningByMediaScanner();
                request.SetAllowedOverRoaming(false);
                request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
                request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, source.LastPathSegment);
                var manager = (DownloadManager)Xamarin.Forms.Forms.Context.ApplicationContext.GetSystemService(Android.Content.Context.DownloadService);
                manager.Enqueue(request);
    
    
                string name = "";
                name = System.IO.Path.GetFileName(url);
    
                return name;
            }
    

    Te amo bro, mucha gracias por el código
    una cosa, sabes como abrir el archivo luego de ser descargado?

    Estoy descarando una nueva versión de mi apk y quiero que al terminar de descargar se pueda abrir solicitando al usuario su previa instalación.
    De ante mano muchísimas gracias. Llevaba buscando este código mucho, mucho tiempo. :D

Sign In or Register to comment.