android webview下载附件几种方法
16lz
2022-07-27
下面几种方式都能下载,记录的是稍作修改后的结果。
首先
webView.setDownloadListener(new MyWebViewDownLoadListener());
然后
1、浏览器,不能传递cookie
private class MyWebViewDownLoadListener implements DownloadListener { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,long contentLength) { Uri uri = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); } }2、DownloadManager,可传递cookie
http://www.2cto.com/kf/201205/132327.html
private class MyWebViewDownLoadListener implements DownloadListener { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,long contentLength) {
//先判断有无权限,否在未授权时app直接跳出
int perm = MainActivity.this.checkCallingOrSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE");if(perm != PackageManager.PERMISSION_GRANTED){Toast.makeText(MainActivity.this,"缺少存储权限:手机-设置-应用管理-权限-存储",Toast.LENGTH_LONG).show();}else {
DownloadManager downloadManager = ((DownloadManager) getSystemService(Activity.DOWNLOAD_SERVICE));
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);request.setVisibleInDownloadsUi(true);
//传cookie
String CookieStr= CookieManager.getInstance().getCookie(url);request.addRequestHeader("Cookie",CookieStr);//contentDisposition本身带有att...一串字符,中文和非中文还不一样String fileName;
if(contentDisposition.indexOf("filename*=UTF-8")!=-1) {
fileName = contentDisposition.substring(contentDisposition.indexOf( "'" ) + 2 , contentDisposition.length());
}
else{fileName = contentDisposition.substring(contentDisposition.indexOf("=") + 1, contentDisposition.length());}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
//过时的,老版用这句,新版用后面那句
request.setShowRunningNotification(true);request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
downloadManager.enqueue(request);}
} }
实际应用中,获取cookie部分是这样写的,这样不必每下一个文件都取一遍
private String CookieStr;private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } public void onPageFinished(WebView view, String url) { CookieStr = CookieManager.getInstance().getCookie(url); super.onPageFinished(view, url); }}
3、thread,这个cookie问题没试过
http://blog.csdn.net/u013210620/article/details/47184511
contentDisposition是为了修改文件名后加的
private class MyWebViewDownLoadListener implements DownloadListener { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,long contentLength) { new HttpThread(url,contentDisposition).start(); } }
public class HttpThread extends Thread { private String url; private String contentDisposition; public HttpThread(String url,String contentDisposition) { super(); this.url = url; this.contentDisposition=contentDisposition; } @Override public void run() { super.run(); try { URL httpUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); InputStream inputStream = connection.getInputStream(); File downFile = null; File sdFile = null; FileOutputStream out = null; String filename; Log.e("TAG", "sddd");
//下面的if有点问题,到这就不执行了,没仔细研究,注释后能过去 //if (Environment.getExternalStorageState().equals(Environment.MEDIA_CHECKING)) { Log.e("TAG", "sd"); downFile = Environment.getExternalStorageDirectory();
//contentDisposition本身带有att...一串字符,中文和非中文还不一样fileName;
if(contentDisposition.indexOf("filename*=UTF-8")!=-1) {
sdFile = new File(downFile, filename); out = new FileOutputStream(sdFile); // } byte [] b = new byte [ 6 * 1024 ]; int len; while ((len = inputStream.read(b)) != - 1 ) { if (out != null ) { out.write(b, 0 , len); } } if (out != null ) { out.close(); } if (inputStream != null ) { inputStream.close(); } Log. e ( "TAG" , "scca" ); } catch (Exception e) { e.printStackTrace(); } }}fileName = contentDisposition.substring(contentDisposition.indexOf( "'" ) + 2 , contentDisposition.length());
}
else{fileName = contentDisposition.substring(contentDisposition.indexOf("=") + 1, contentDisposition.length());}
更多相关文章
- android中文api(89)——ViewManager
- Android(安卓)给 app默认权限(不弹窗申请权限)
- Android(安卓)中文API(86)——ResourceCursorAdapter
- Android中文API(144) —— JsonWriter
- android 中文api (62) —— ViewSwitcher.ViewFactory
- android api 中文 (74)—— AdapterView.AdapterContextMenuInfo
- Android(安卓)中文 API (27) —— SeekBar.OnSeekBarChangeListene
- android 中使用TextView实现分段显示不同颜色的字符串
- android中json文件的写法