android - How to force a file to download in-app instead of showing up in the default browser -
I have created an Android application with WebView, but I am facing the only problem, that the files are opening instead of the default browser. downloading.
This is the webactivity code:
extends the web-activity activity of the public class {Private WebWeb Webview; Crate on Public Zero (Bundle Saved Instantstate) {Super. NET (Saved Instantstate); SetContentView (R.layout.main); Find webview = (webview) VVBIID (R.D.WebWeight Electricity 01); WebView.getSettings () setJavaScriptEnabled (true). webView.setWebViewClient (New myWebClient () {@Override Public Zero onPageFinished (WebView to view, string URL) {findViewById (R.id.imageLoading1) .setVisibility (View.GONE); findViewById (R.id.webView01) .setVisibility (See .VISIBLE);}}); WebView.loadUrl ("domain"); webView.setDownloadListener (New DownloadListener) {Public Zero onDownloadStart (String URL, String UserAgent, String contentDisposition, String Mime Type, Long ContentLength) {Intent I = New Intent (Intent.ACTION_VIEW); i.setData (Uri.parse (URL );; Initiation (i);}}); } Public class myWebClient extends WebViewClient {@Override Public Zero (Viewing WebView, String URL, Bitmap Favicon) {super.onPageStarted (View, URL, Favicon) onPageStarted; } @ Override public boolean should require override URLloading (webview view, string url) {view.loadUrl (url); Back true; }} @Override Public Boolean OnKeyDown (Integer Keycode, KeyEvent Event) {if ((encoded == KeyEvent.KEYCODE_BACK) & amp; WebView.canGoBack ()} {webView.goBack (); Back true; } Return super.Condondon (keycode, event); The browser app is open because it is the appropriate app to handle ;}}
Intent is holding a URL as its data, which you are using in your DownloadListener .
If you do not want to fire the browser, open a InputStream with your URL from your DownloadListener and get a byte from the file.
FileOutputStream fout = null; Try {in = new BufferedInputStream (new URL (urlString) .openstream ()); Fout = New FileOutputStream (filename); Last byte data [] = new byte [1024]; integer number; While ((calculation = in read (data, 0, 1024)) -1) {fout.write (data, 0, count); }} Finally {if (in! = Null) {in.close (); } If (fout! = Null) {fout.close (); }}}
Comments
Post a Comment