Solution of WebView Attachment Problem in android

Keywords: Android Google iOS Mobile

Many app projects now have the need to embed html, so there are many benefits in designing products. An HTML can run on many platforms at the same time (android, ios, web side).
From an android perspective, the component used to load html is webview.

We all know the benefits of webview: 1. It can display and render web pages directly.

2. Direct display of web page webview can be directly laid out with html files (on the network or in local assets)

3. Interactive calls with JavaScript

But at the same time, it also brings us some problems, such as some hyperlink attachments on html: xxx.pdf, xxx.doc and so on.

ios can directly parse the loaded files, which is estimated to be the reason why the ios browser kernel is powerful, but android by default is unable to parse and display such attachments.

android's web view is only a package of the browser's kernel. It does not have the function of opening word,excel,ppt,pdf files. Even if it can be opened, it must use the third party's special plug-ins or poi libraries.

The effect of ios is that it can open the attachment file directly by default in the current interface, and the product thinks that this android can certainly do the same. (Unable to explain the product, too much explanation is your own dish.)

But when I searched for information online, I found that android could parse pdf Online provided by google if it wanted to read online.

Java code


WebView webview = (WebView) findViewById(R.id.webview);  
webview.getSettings().setJavaScriptEnabled(true);   
String pdf ="http://..../a.pdf";  
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf);  

Originally very happy, but carefully observe the url, is basically pulling, this visit is Google ah, how can ensure that every user's mobile phone can connect to vpn access to google? So this method is not feasible at all.


There are two ways to continue research.

1. If you want to read online, but do not want to use the online parsing provided by google, you need to install the corresponding plug-ins locally, such as pdf plug-ins, word plug-ins and so on. Just like playing flash with IE and firefox, you need to install flash plug-ins.  

2. If you want to open a local PDF file or something directly with webview, you can use apache poi to parse word,excel,ppt,pdf, etc. That is to say, you need to use poi to develop your WebView program.

The first one is to install the plug-in separately, and the second one is to parse the file with a third-party framework, parse out an html, and then display it.

In a word, both of these effects are not very appropriate.


When I had no idea, I did a test with qqq, sent our html link to my mobile phone qqq, then clicked on the attachment on html on qqq, found that it was downloaded first, and then prompted to open with related software on the mobile phone, such as tripartite browser (UC browser, QQ browser) and office.

Later, we did not do online parsing directly open, but also do like qq download first and then rely on third-party software to open. I think it's more reasonable (1. It's not necessary to spend so much time on this function, and 2. Follow the mainstream software processing method).


The code of the webview download file:


package com.baobao.webview;

import com.cx.httpwebview.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private TextView textView;
	private WebView webView;
	
	/** mUrl Test the connection url, which can be replaced by its own html path.
	 *  You can put pdf attachments or doc attachments on html on your server.
	 *  If it is a document file, it calls the system browser to download. If multiple browsers are installed, the selection interface will pop up. If it is a picture, it opens the effect directly.*/
	private String mUrl = "http://shouji.baidu.com";
	
	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		
		textView = (TextView) findViewById(R.id.textView1);
		webView = (WebView) findViewById(R.id.webView1);
		
		webView.getSettings().setJavaScriptEnabled(true);
		webView.setWebViewClient(new WebViewClient(){ 
		     public boolean shouldOverrideUrlLoading(WebView view, String url) {
                 view.loadUrl(url);
                 return true;
		     }
		}); 
		webView.setWebChromeClient(new WebChromeClient(){
			@Override
			public void onReceivedTitle(WebView view, String title) {
				textView.setText(title);
				super.onReceivedTitle(view, title);
			}
		});
		webView.setDownloadListener(new MyDownloadStart());
		webView.loadUrl(mUrl);
	}
	
	class MyDownloadStart implements DownloadListener{

		@Override
		public void onDownloadStart(String url, String userAgent,
				String contentDisposition, String mimetype, long contentLength) {
			//Call your own download method
//			new HttpThread(url).start();
			//Call System Browser Download
			Uri uri = Uri.parse(url);  
	        Intent intent = new Intent(Intent.ACTION_VIEW, uri);  
	        startActivity(intent);      
		}
	}
}

The core is the above lines of code, and the demo file is attached at the end. Habit to write a blog to a small demo, even a very simple function, feel more intuitive.


DEMO Source Download

If you have other questions, you can join our qq group: 454430053

Posted by Awanka on Fri, 29 Mar 2019 04:12:30 -0700