Android Basics Getting Started tutorial--7.5.4 webview file Download

Source: Internet
Author: User

Android Basics Getting Started tutorial--7.5.4 webview file Download

tags (space delimited): Android Basics Getting Started Tutorial

Introduction to this section

This section introduces you to the knowledge of WebView download files, when we are using a common browser, such as UC,
When we click on a downloadable link, it will be downloaded, webview as a browser-like component,
Of course also support download, we can write our own download process, set the downloaded file where, to what filename
Save, of course, can also call other built-in browser to download, such as CHROME,UC and so on!
Below to show you how to use!

1. Call other browsers to download the file:

This is very simple, we only need to set Setdownloadlistener for WebView, and then rewrite Downloadlistener's
Ondownloadstart, then write a intent inside, then startactivity the corresponding activity can!

The key code is as follows :

Wview.Setdownloadlistener (NewDownloadlistener () {@Override Public voidOndownloadstart (StringUrlStringUserAgent,StringContentdisposition,StringMimeType, long ContentLength) {Log.E"HEHE","Start Download"); URI URI=Uri.Parse (URL); Intent Intent= NewIntent (Intent.Action_view,uri);            StartActivity (Intent); }        });

If you have more than one browser in your phone, a dialog will open for you to select one of the browsers to download.

2. Write your own thread to download the file

Of course, you may not want to put the download file in the default path, or you want to define your own file name, etc., you can write your own
A thread to download the file, implementing the sample code as follows:

Core Code :

We write a separate download thread class ourselves:

Downloadthread.java

/** * Created by Jay on 2015/9/14 0014. * * Public  class downloadthread implements Runnable {    PrivateString Dlurl; Public Downloadthread(String Dlurl) { This. Dlurl = Dlurl; }@Override     Public void Run() {LOG.E ("HEHE","Start Download ~~~~~"); InputStream in =NULL; FileOutputStream Fout =NULL;Try{URL Httpurl =NewURL (Dlurl);            HttpURLConnection conn = (httpurlconnection) httpurl.openconnection (); Conn.setdoinput (true); Conn.setdooutput (true);            in = Conn.getinputstream (); File DownloadFile, Sdfile;if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {LOG.E ("HEHE","SD card can be written");                DownloadFile = Environment.getexternalstoragedirectory (); Sdfile =NewFile (DownloadFile,"csdn_client.apk"); Fout =NewFileOutputStream (Sdfile); }Else{LOG.E ("HEHE","SD card does not exist or is not writable"); }byte[] buffer =New byte[1024x768];intLen while(len = in.read (buffer))! =-1) {fout.write (buffer,0, Len); }        }Catch(Exception e)        {E.printstacktrace (); }finally{if(In! =NULL) {Try{In.close (); }Catch(IOException e)                {E.printstacktrace (); }            }if(Fout! =NULL) {Try{Fout.close (); }Catch(IOException e)                {E.printstacktrace (); }}} LOG.E ("HEHE","Download Complete ~ ~ ~ ~"); }}

Then create and start the thread in Mainactivity.java :

 Wview    Setdownloadlistener (new  Downloadlistener () {@Override public  void  ondownloadstart (string  URL, string  useragent, string  contentdisposition, string  mimetype, long contentlength) {log   E ( "HEHE" ,  "ondownloadstart called: Download Link:"  +  URL); new  thread  (new  downloadthread (URL))         Start (); }    });

Operation result :

We open the SD card to see that the downloaded file has been quietly lying in the SD card:

Precautions:

OK, in addition, do not forget to write the SD card read and Write permissions and Internet access to the network permissions:

<uses-permission android:name="android.permission.INTERNET"/>    <!-- 在SDCard中创建与删除文件权限 -->    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>    <!-- 往SDCard写入数据权限 -->    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Also, in= Conn.getinputstream (); to write after Conn set everything!! Remember, otherwise you can't read anything!

This section summarizes:

This section is very simple, the code is not posted, in fact, it is setdownloadlistener this thing, their own rewrite under
Ondownloadstart method to handle the download process ~, this section is here, thank you ~

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Basics Getting Started tutorial--7.5.4 webview file Download

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.