首先如果仅仅是想下载文件的话,这里介绍一个比较简单的方法
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
如果想要打开文件的话,比如服务器上有个xls文件。由于内嵌浏览器不支持插件功能,所以我们换种思路解决一下。将文件下载下来以后提示用户用相应的程序打开,如果没有能打开xls文件的程序的话,那就没有办法了
步骤如下:
1、设置webViewClient
webView.setWebViewClient(webViewClient);
2、重写webViewClient的shouldOverrideUrlLoading方法
private WebViewClient webViewClient = new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Attach attach = new Attach(PriceTableContentActivity.this, url);
try {
attach.openServerFile();
} catch (Exception e) {
}
return true;
}
};
3、写Attach 类
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import com.highsoft.mobile2.http.HttpService;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
/**
* 下载文件辅助类
* @author chenpeng
*
*/
public class Attach {
private final static String DIRECTPATH = "/data/data/com.highsoft.mobile2/files/";//存储文件地址,格式/data/data/包名/files/
private String downloadUrl;// 下载路径
private String fileName;
private Context context;
public Attach(Context context, String downloadUrl) {
this.context = context;
this.downloadUrl = downloadUrl;
fileName = "a." + getFileExtension(downloadUrl);
}
public void openServerFile() {
new SubmitComAsyc(context).execute();
}
class SubmitComAsyc extends AsyncTask<String, Void, String> {
private Context context;
ProgressDialog pd;
public SubmitComAsyc(Context context) {
this.context = context;
pd = new ProgressDialog(context);
pd.setMessage("loading..");
pd.setIndeterminate(true);
pd.setCancelable(true);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setButton("cancle", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SubmitComAsyc.this.cancel(true);
}
});
pd.show();
}
protected String doInBackground(String... arg) {
InputStream is = null;
OutputStream os = null;
try {
is = getInputStreamByUrl(downloadUrl);
os = context.openFileOutput(fileName,
Context.MODE_WORLD_READABLE);
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) != -1) {
os.write(buf, 0, len);
}
os.flush();
} catch (Exception e) {
e.printStackTrace();
return "fail";
} finally {
try {
if (is != null)
is.close();
if (os != null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "ok";
}
protected void onPostExecute(String value) {
pd.dismiss();
// Toast.makeText(context, value, Toast.LENGTH_SHORT).show();
Intent intent = new Intent("android.intent.action.VIEW");
File file = new File(DIRECTPATH + fileName);
String mimeType = "*/*";
String suffix = getFileExtension(downloadUrl);
if(suffix.equals("doc")){
mimeType = "application/msword";
}else if (suffix.equals("xls")) {
mimeType = "application/msexcel";
}else if (suffix.equals("pdf")) {
mimeType = "application/pdf";
}else if (suffix.equals("tif")) {
mimeType = "image/tiff";
}
intent.setDataAndType(Uri.fromFile(file), mimeType);
context.startActivity(intent);
}
}
public static InputStream getInputStreamByUrl(String url) {
HttpGet httpget = new HttpGet(url);
HttpClient httpClient = HttpService.getInstance().getNewHttpClient();
try {
HttpResponse httpResponse = httpClient.execute(httpget);
InputStream is = httpResponse.getEntity().getContent();
return is;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 取得加载文件的后缀名
private String getFileExtension(String strFileName) {
File myFile = new File(strFileName);
String strFileExtension = myFile.getName();
strFileExtension = (strFileExtension.substring(strFileExtension
.lastIndexOf(".") + 1)).toLowerCase();
if (strFileExtension == "") {
/* 若无法顺利取得扩展名,默认为.dat */
strFileExtension = "dat";
}
return strFileExtension;
}
}