Until today I never thought in order to download file from Urls, one needs to implement the Interface Download Listener on his Activity class. I have a webview in Xamarin Android, and I can't seem to download files..
figured that's because i haven't implemented this interface Download Listener on my class..So i gave it a try but i cant seem to connect the Interface implementing method with the OnDwnloadStart
method, i figure when i request a download from a web page then the IDownloadListener method does nothing coz it has no code....But the code that should handle a download request is in the OnDownloadStart
method with url, ContentDisposition and mimetype
parameters,Any hep making the interface call OnDownloadStart
method will surely be appreciated..Here is the code that i used...
class Internet : AppCompatActivity,IDownloadListener
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.Browser);
//Webview definition
webview = this.FindViewById<WebView>(Resource.Id.webview1);
//webview properties
webview.SetWebViewClient(new WebViewClient());
WebSettings webSettings = webview.Settings;
webSettings.SetSupportMultipleWindows(true);
webSettings.SetEnableSmoothTransition(true);
webSettings.JavaScriptEnabled = true;
webSettings.DomStorageEnabled = true;
webSettings.AllowFileAccessFromFileURLs = true;
// webSettings.JavaScriptEnabled.
webview.SetDownloadListener(this);
}
//Interface Download Listener Method
public interface IDownloadListener : Android.Runtime.IJavaObject, IDisposable
{
//I got nothing here and this is what i think needs to call OndownloadStart
}
//implementing OnDownloadStart Method
public void OnDownloadStart(string url, string userAgent, string contentDisposition, string mimetype, long contentLength)
{
DownloadManager.Request request = new DownloadManager.Request(Android.Net.Uri.Parse(url));
String cookies = CookieManager.Instance.GetCookie(url);
request.AddRequestHeader("cookie", cookies);
request.AddRequestHeader("User-Agent", userAgent);
request.SetDescription("Downloading file to crn folder...");
request.SetTitle(URLUtil.GuessFileName(url, contentDisposition, mimetype));
request.AllowScanningByMediaScanner();
request.SetNotificationVisibility(Android.App.DownloadVisibility.VisibleNotifyCompleted);
File dest = new File(Android.OS.Environment.RootDirectory + "download");
if (!dest.Exists())
{
if (!dest.Mkdir())
{ Log.Debug("TravellerLog ::", "Problem creating Image folder"); }
}
request.SetDestinationInExternalFilesDir(Application.Context, "download",
URLUtil.GuessFileName(url, contentDisposition, mimetype));
DownloadManager manager =
(DownloadManager)GetSystemService(Android.App.Application.DownloadService);
manager.Enqueue(request);
//Notify if success with BroadCast Receiver
}
}
Which part of this code is running wrong? Any help is appreciated..