Android Browser--use WebView for file download

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.