Basic Android tutorial-7.5.4 WebView file download, 7.5.4webview

Source: Internet
Author: User

Basic Android tutorial-7.5.4 WebView file download, 7.5.4webview
Basic Android tutorial-7.5.4 WebView File Download

Tags (separated by spaces): basic Android tutorial

This section introduces

This section describes how to download files from WebView. When using a common browser, such as UC,
When we click a link available for download, We will download the WebView as a browser-like component,
Of course, download is also supported. We can write the download process by ourselves and set where the downloaded file is stored and what file name
Save, of course, you can also call other built-in browsers to download, such as Chrome and UC!
The following is a demonstration of usage!

1. Call other browsers to download files:

This is very simple. We only need to set setDownloadListener for WebView, and then rewrite the DownloadListener
OnDownloadStart, write an Intent in it, and then startActivity corresponding Activity!

The key code is as follows::

WView. setDownloadListener (new DownloadListener () {@ Override public void onDownloadStart (String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {Log. e ("HEHE", "Start download"); Uri uri = Uri. parse (url); Intent intent = new Intent (Intent. ACTION_VIEW, uri); startActivity (intent );}});

If multiple browsers exist in your mobile phone, a dialog box will be opened for you to select one of the browsers to download ~

2. download files by writing a thread on your own

Of course, you may not want to put the downloaded file in the default path, or want to define your own file name, etc. You can write it by yourself.
Sample Code for downloading an object in a thread is as follows:

Core code:

We have another thread class for download:

DownLoadThread. java

/*** Created by Jay on 0014. */public class DownLoadThread implements Runnable {private String dlUrl; public DownLoadThread (String dlUrl) {this. dlUrl = dlUrl;} @ Override public void run () {Log. e ("HEHE", "Start to download ~~~~~ "); InputStream in = null; FileOutputStream fout = null; try {URL httpUrl = new URL (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 writable"); downloadFile = Environment. getExt ErnalStorageDirectory (); sdFile = new File (downloadFile, "csdn_client.apk"); fout = new FileOutputStream (sdFile);} else {Log. e ("HEHE", "SD card does not exist or cannot be read/written");} byte [] buffer = new byte [1024]; int len; 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 completed ~~~~ ");}}

ThenMainActivity. javaCreate and start this thread:

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 ();}});

Running result:

When we open the SD card, we can see that the downloaded file has been quietly lying in the SD card:

Notes:

Okay. In addition, do not forget the read and write permissions for the SD card and Internet access permissions:

<Uses-permission android: name = "android. permission. INTERNET"/> <! -- Create and delete file permissions in SDCard --> <uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/> <! -- Write data permission to SDCard --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>

And,In = conn. getInputStream (); It should be written after conn sets everything !! Remember, otherwise nothing can be read!

Summary:

This section is very simple and the code will not be pasted out. It is actually the setDownloadListener, which can be rewritten by yourself.
The onDownloadStart method is used to process the download process ~, This section is here. Thank you ~

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.