本文实例为大家分享了AndroID使用webVIEw长按保存下载网络图片的具体代码,供大家参考,具体内容如下
最近发现在webVIEw的setonLongClickListener中可以获取到WebVIEw.HitTestResult,根据获取的HitTestResult的Type来判断做不同的处理。通过判断Type的类型获取点击图片的url,然后把图片下载到本地,发送广播通知系统图库进行更新,在系统图库中查看下载的图片。运行Demo在网页中对图片做长按点击即可下载网络图片
直接上代码:
下面附有Demo下载:点击打开链接
package demo.sam.webvIEw_demo; import androID.app.Activity;import androID.app.Dialog;import androID.content.Context;import androID.content.DialogInterface;import androID.content.Intent;import androID.net.Uri;import androID.os.Handler;import androID.os.Message;import androID.provIDer.MediaStore;import androID.support.v7.app.AlertDialog;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.text.Editable;import androID.util.Log;import androID.vIEw.KeyEvent;import androID.vIEw.VIEw;import androID.webkit.WebChromeClIEnt;import androID.webkit.WebResourceRequest;import androID.webkit.WebSettings;import androID.webkit.WebVIEw;import androID.webkit.WebVIEwClIEnt;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Progressbar;import androID.Widget.Toast; import java.io.fileNotFoundException; public class MainActivity extends Activity { private Progressbar progress; private WebVIEw webVIEw; private EditText editText; private button click; private Context context; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); context= this; initVIEw(); initData(); initListener(); } private voID initData() { WebSettings settings = webVIEw.getSettings(); settings.setJavaScriptEnabled(true); settings.setUseWIDeVIEwPort(true);//设置此属性,可任意比例缩放 settings.setLoaDWithOverviewmode(true); // 使页面支持缩放 settings.setBuiltInZoomControls(true); settings.setSupportZoom(true); //支持自动加载图片 settings.setLoadsImagesautomatically(true); settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.norMAL);// 排版适应屏幕 // 缩放按钮 settings.setdisplayZoomControls(false); webVIEw.setWebVIEwClIEnt(new WebVIEwClIEnt(){ // 页面在当前页面跳转 @OverrIDe public boolean shouldOverrIDeUrlLoading(WebVIEw vIEw,WebResourceRequest request) { return super.shouldOverrIDeUrlLoading(vIEw,request); } // 页面加载结束 @OverrIDe public voID onPageFinished(WebVIEw vIEw,String url) { super.onPageFinished(vIEw,url); if(progress!=null){ progress.setVisibility(VIEw.GONE); } } }); } private voID initVIEw() { progress = (Progressbar) findVIEwByID(R.ID.progress); webVIEw = (WebVIEw) findVIEwByID(R.ID.webVIEw); editText = (EditText) findVIEwByID(R.ID.url); click = (button) findVIEwByID(R.ID.click); } private voID initListener() { // 网页加载进度显示 webVIEw.setWebChromeClIEnt(new WebChromeClIEnt(){ @OverrIDe public voID onProgressChanged(WebVIEw vIEw,int newProgress) { super.onProgressChanged(vIEw,newProgress); progress.setVisibility(VIEw.VISIBLE); progress.setProgress(newProgress); } }); click.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { Log.e("输入的网站",editText.getText().toString().trim()); webVIEw.loadUrl(editText.getText().toString().trim()); } }); // 长按点击事件 webVIEw.setonLongClickListener(new VIEw.OnLongClickListener() { @OverrIDe public boolean onLongClick(VIEw vIEw) { final WebVIEw.HitTestResult hitTestResult = webVIEw.getHitTestResult(); // 如果是图片类型或者是带有图片链接的类型 if(hitTestResult.getType()== WebVIEw.HitTestResult.IMAGE_TYPE|| hitTestResult.getType()== WebVIEw.HitTestResult.SRC_IMAGE_ANCHOR_TYPE){ // d出保存图片的对话框 AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("提示"); builder.setMessage("保存图片到本地"); builder.setPositivebutton("确认",new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialogInterface,int i) { String url = hitTestResult.getExtra(); // 下载图片到本地 DownPicUtil.downPic(url,new DownPicUtil.DownFinishListener(){ @OverrIDe public voID getDownPath(String s) { Toast.makeText(context,"下载完成",Toast.LENGTH_LONG).show(); Message msg = Message.obtain(); msg.obj=s; handler.sendMessage(msg); } }); } }); builder.setNegativebutton("取消",new DialogInterface.OnClickListener() { // 自动dismiss @OverrIDe public voID onClick(DialogInterface dialogInterface,int i) { } }); AlertDialog dialog = builder.create(); dialog.show(); } return true; } }); webVIEw.loadUrl("http://www.baIDu.com"); } Handler handler =new Handler(){ @OverrIDe public voID handleMessage(Message msg) { super.handleMessage(msg); String picfile = (String) msg.obj; String[] split = picfile.split("/"); String filename = split[split.length-1]; try { MediaStore.Images.Media.insertimage(getApplicationContext().getContentResolver(),picfile,filename,null); } catch (fileNotFoundException e) { e.printstacktrace(); } // 最后通知图库更新 getApplicationContext().sendbroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_file,Uri.parse("file://" + picfile))); Toast.makeText(context,"图片保存图库成功",Toast.LENGTH_LONG).show(); } }; // 监听返回键返回网页的上一层 @OverrIDe public boolean onKeyDown(int keyCode,KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK && webVIEw.canGoBack()){ if(webVIEw != null){ webVIEw.goBack(); return true; } } return super.onKeyDown(keyCode,event); } }
图片下载的工具类
import androID.os.AsyncTask;import androID.os.Environment;import androID.util.Log; import java.io.file;import java.io.fileNotFoundException;import java.io.fileOutputStream;import java.io.IOException;import java.io.inputStream;import java.io.OutputStream;import java.net.URL;import java.util.Random; /** * 图片下载的工具类 */public class DownPicUtil { /** *下载图片,返回图片的地址 * @param url */ public static voID downPic(String url,DownFinishListener downFinishListener){ // 获取存储卡的目录 String filePath = Environment.getExternalStorageDirectory().getPath(); file file = new file(filePath+file.separator+"webVIEwCache"); if(!file.exists()){ file.mkdir(); } loadPic(file.getPath(),url,downFinishListener); } private static voID loadPic(final String filePath,final String url,final DownFinishListener downFinishListener) { Log.e("下载图片的url",url); new AsyncTask<VoID,VoID,String>(){ String filename; inputStream is; OutputStream out; @OverrIDe protected String doInBackground(VoID... voIDs) { // 下载文件的名称 String[] split = url.split("/"); String newString = split[split.length - 1]; filename =newString.substring(newString.length()-20,newString.length()-1) ; // 创建目标文件,不是文件夹 file picfile = new file(filePath + file.separator + filename); if(picfile.exists()){ return picfile.getPath(); } try { URL picUrl = new URL(url); //通过图片的链接打开输入流 is = picUrl.openStream(); if(is==null){ return null; } out = new fileOutputStream(picfile); byte[] b=new byte[1024]; int end ; while ((end=is.read(b))!=-1){ out.write(b,end); } Log.e("OK??","----------"); if(is!=null){ is.close(); } if(out!=null){ out.close(); } } catch (fileNotFoundException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } return picfile.getPath(); } @OverrIDe protected voID onPostExecute(String s) { super.onPostExecute(s); if(s!=null){ downFinishListener.getDownPath(s); } } }.execute(); } //下载完成回调的接口 public interface DownFinishListener{ voID getDownPath(String s); }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的Android使用webView长按保存下载网络图片全部内容,希望文章能够帮你解决Android使用webView长按保存下载网络图片所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)