Two Methods of Using WebView to Realize File Download Function in Android Programming

  • 2021-08-21 21:20:32
  • OfStack

In this paper, two methods of Android programming using WebView to realize file download function are described. Share it for your reference, as follows:

In the application, we usually use the file download function. Generally, we write a download operation tool class to perform the download function in asynchronous tasks.

Today, let's look at how to use the file download function of WebView!

Method 1. Customize the download operation

Step 1 Layout first


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ldm="http://schemas.android.com/apk/res/com.ldm.learn"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<WebView
android:id="@+id/test_wv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp" />
</RelativeLayout>

2. Implement custom download tools to operate asynchronous thread classes:


public class DownLoadThread extends Thread {
private String downLoadUrl;
private Context context;
private FileOutputStream out = null;
private File downLoadFile = null;
private File sdCardFile = null;
private InputStream in = null;
public DownLoadThread(String downLoadUrl, Context context) {
super();
this.downLoadUrl = downLoadUrl;
this.context = context;
}
@Override
public void run() {
try {
URL httpUrl = new URL(downLoadUrl);
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
 conn.setDoInput(true);//  If you intend to use  URL  Connect for input, set the  DoInput  Flag is set to  true ; If you do not intend to use it, set to  false . The default value is  true . 
conn.setDoOutput(true);//  If you intend to use 
 URL  Connect to output, the  DoOutput  Flag is set to  true ; If you do not intend to use it, set to  false . The default value is  false . 
in = conn.getInputStream();
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
 Toast.makeText(context, "SD The card is not available! ", Toast.LENGTH_SHORT).show();
return;
}
downLoadFile = Environment.getExternalStorageDirectory();
sdCardFile = new File(downLoadFile, "download.apk");
out = new FileOutputStream(sdCardFile);
byte[] b = new byte[1024];
int len;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
 }
}

3. File download


public class MainActivity extends Activity {
private WebView test_wv;
private String downLoadUrl = "http://as.baidu.com/a/rank?cid=101&s=1&f=web_alad";
 @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.test_wv
 = (WebView) findViewById(R.id.test_wv);
test_wv.loadUrl(downLoadUrl);
test_wv.setWebViewClient(new WebViewClient()
 {
@Override
public boolean shouldOverrideUrlLoading(WebView
 view, String url) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
});
// To achieve WebView File download, realize this monitoring on ok
test_wv.setDownloadListener(new
 DownloadListener() {
@Override
public void onDownloadStart(String
 url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Log.v("ldm", url);
if (url.endsWith(".apk")) {// Determine whether it is .apk File path at the end 
new DownLoadThread(url, MainActivity.this).start();
}
}
});
}
}

Method 2: Download by the system itself (the download progress bar will be displayed in the notification bar)

Just rewrite this method as follows:


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

More readers interested in Android can check the topics of this site: "Summary of Android File Operation Skills", "Summary of Android View View Operation Skills", "Summary of activity Operation Skills of Android Programming", "Summary of layout Layout Skills", "Introduction and Advanced Tutorial of Android Development", "Summary of Android Resource Operation Skills" and "Summary of Android Control Usage"

I hope this article is helpful to everyone's Android programming.


Related articles: