Download monitor for current webview settings
Mcurrentwebview.setdownloadlistener (new Downloadlistener () { @Override public void Ondownloadstart (finallong contentlength) { // TODO Implement download Logic log.e ("Ondownloadstart", "url===" + URL + "---useragent=" + useragent + "---contentdisposition=" + cont Entdisposition + "---mimetype=" + mimetype + "---contentlength=" + contentlength); }});
Download the file Core code:
Httpparams params =NewBasichttpparams (); Httpconnectionparams.setconnectiontimeout (params,5 * 1000); Httpconnectionparams.setsotimeout (params,5 * 1000); HttpGet HttpGet=Newhttpget (URL);Try{File File=NewFile (Environment.getexternalstoragedirectory (), fileName); if(!file.exists ()) {File.createnewfile (); } randomaccessfile randomfile=NewRandomaccessfile (file, "RW"); HttpResponse Response=Newdefaulthttpclient (params). Execute (httpget); Httpentity Entity=response.getentity (); InputStream in=entity.getcontent (); Randomfile.seek (Randomfile.length ()); byte[] buffer =New byte[1024]; intLenght = 0; while((lenght = in.read (buffer)) > 0) {randomfile.write (buffer,0, lenght); Debugtracetool.debugtracee ( This, "file length = =" +randomfile.length ()); } randomfile.close (); Httpget.abort (); } Catch(Exception e) {e.printstacktrace ();}
Points to note:
1. You need to start a single thread and cannot perform a file download operation on the main thread.
2. Download the file name, length limit, the recommended file name length control at 100. Prevents the Ioexception:open failed:enametoolong (file name too long) error from appearing, causing the downloaded task to not start correctly. Cause: There is no limit to the length of the file name in the Java language specification. However, the operating system has a limit on the length of the file name, the most common is 255 bytes, this limit length includes the file name suffix, such as. mp3,.mkv.
Android Browser-use WebView for file download