示例#1
0
        public void DownloadFile(string uri, string fileName)
        {
            var contentUri = Android.Net.Uri.Parse(uri);
            var request    = new Android.App.DownloadManager.Request(contentUri);

            request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, fileName);
            request.AllowScanningByMediaScanner();
            request.SetNotificationVisibility(Android.App.DownloadVisibility.VisibleNotifyCompleted);
            var downloadManager = (Android.App.DownloadManager)Forms.Context.GetSystemService(Android.Content.Context.DownloadService);

            downloadManager.Enqueue(request);
        }
示例#2
0
        private void Download(string Url, string filename)
        {
            Android.Net.Uri contentUri            = Android.Net.Uri.Parse(Url);
            Android.App.DownloadManager.Request r = new Android.App.DownloadManager.Request(contentUri);
            r.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, filename);
            r.AllowScanningByMediaScanner();
            r.SetNotificationVisibility(Android.App.DownloadVisibility.VisibleNotifyCompleted);
            Android.App.DownloadManager dm = (Android.App.DownloadManager)Xamarin.Forms.Forms.Context.GetSystemService(Android.Content.Context.DownloadService);

            dm.Enqueue(r);

            var localFolder = Android.OS.Environment.DirectoryDownloads;
            var MyFilePath  = $"file://{localFolder}/{Filename}";

            Toast.MakeText(this, MyFilePath, ToastLength.Long).Show();
        }
        public async Task<bool> Download(string uri, string filename)
        {

            string downloadedFolder = "/storage/emulated/0/Download/";
            if (App.DownloadsPath != null && !string.IsNullOrEmpty(App.DownloadsPath))
            {
                downloadedFolder = App.DownloadsPath + "/";
            }

			string fileExtenstion = Path.GetExtension(filename);

			if (fileExtenstion == ".jpg" || fileExtenstion == ".jpeg" || fileExtenstion == ".png")
				return false;

            if (System.IO.File.Exists(downloadedFolder + filename))
            {
                string downloadedUri = "file://" + downloadedFolder + filename;
                Java.IO.File file = new Java.IO.File(new Java.Net.URI( downloadedUri ));
                Intent videoPlayerActivity = new Intent(Intent.ActionView);
                videoPlayerActivity.SetDataAndType(Android.Net.Uri.FromFile(file), "video/*");
                Activity activity = Forms.Context as Activity;
                activity.StartActivity(videoPlayerActivity);
                return true;
            }
          
            App.DownloadID = 0;

            Android.Net.Uri contentUri = Android.Net.Uri.Parse(uri);

            Android.App.DownloadManager.Request r = new Android.App.DownloadManager.Request(contentUri);


            r.SetDestinationInExternalPublicDir(Android.OS.Environment.ExternalStorageDirectory.ToString(), filename);

            r.AllowScanningByMediaScanner();

            r.SetNotificationVisibility(Android.App.DownloadVisibility.VisibleNotifyCompleted);

            Android.App.DownloadManager dm = (Android.App.DownloadManager)Xamarin.Forms.Forms.Context.GetSystemService(Android.Content.Context.DownloadService);

            App.DownloadID = dm.Enqueue(r);

            AndHUD.Shared.Show(MainActivity.GetMainActivity(), "Dowloading Media...", -1, MaskType.Clear, null, () => { dm.Remove(App.DownloadID); }, true, () => { dm.Remove(App.DownloadID); });


            return true;
        }
        /// <summary>
        /// Use DownloadManager of Android to Download. User Can se status on Action Bar
        /// </summary>
        /// <param name="uri">Full url to download</param>
        /// <param name="filename">File to download</param>
        public void Download(string uri, string filename)
        {
            Android.Net.Uri contentUri = Android.Net.Uri.Parse(uri);

            Android.App.DownloadManager.Request r = new Android.App.DownloadManager.Request(contentUri);


            r.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, filename);

            r.AllowScanningByMediaScanner();

            r.SetNotificationVisibility(Android.App.DownloadVisibility.VisibleNotifyCompleted);

            Android.App.DownloadManager dm = (Android.App.DownloadManager)Xamarin.Forms.Forms.Context.GetSystemService(Android.Content.Context.DownloadService);

            dm.Enqueue(r);
        }
        public void Download(string uri, string filename)
        {
            var contentUri   = Android.Net.Uri.Parse(uri);
            var r            = new Android.App.DownloadManager.Request(contentUri);
            var downloadpath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;

            var file = new File(Path.Combine(downloadpath, filename));

            if (file.Exists())
            {
                return;
            }
            r.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, filename);
            r.AllowScanningByMediaScanner();
            r.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
            dm      = (Android.App.DownloadManager)Forms.Context.GetSystemService(Context.DownloadService);
            _fileId = dm.Enqueue(r);
        }