在项目中遇到一个保存webview中二维码图片的问题,做个笔记。
效果如图:
用到webview的三个方法:
_mWebview.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
}
});
WebView.HitTestResult result = ((WebView) v).getHitTestResult();
int type = result.getType();
type有这几种类型:
WebView.HitTestResult.UNKNOWN_TYPE 未知类型
WebView.HitTestResult.PHONE_TYPE 电话类型
WebView.HitTestResult.EMAIL_TYPE 电子邮件类型
WebView.HitTestResult.GEO_TYPE 地图类型
WebView.HitTestResult.SRC_ANCHOR_TYPE 超链接类型
WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE 带有链接的图片类型
WebView.HitTestResult.IMAGE_TYPE 单纯的图片类型
WebView.HitTestResult.EDIT_TEXT_TYPE 选中的文字类型
String imgurl = result.getExtra();
通过判断类型来做不同的显示效果,在这里我只对图片处理,长按图片弹出一个PopWindow,效果自己写。
自定义PopWindow——ItemLongClickedPopWindow.java
public class ItemLongClickedPopWindow extends PopupWindow {
/** * 书签条目弹出菜单 * @value * {@value} * */
public static final int FAVORITES_ITEM_POPUPWINDOW = 0;
/** * 书签页面弹出菜单 * @value * {@value} * */
public static final int FAVORITES_VIEW_POPUPWINDOW = 1;
/** * 历史条目弹出菜单 * @value * {@value} * */
public static final int HISTORY_ITEM_POPUPWINDOW = 3;
/** * 历史页面弹出菜单 * @value * {@value} * */
public static final int HISTORY_VIEW_POPUPWINDOW = 4;
/** * 图片项目弹出菜单 * @value * {@value} * */
public static final int IMAGE_VIEW_POPUPWINDOW = 5;
/** * 超链接项目弹出菜单 * @value * {@value} * */
public static final int ACHOR_VIEW_POPUPWINDOW = 6;
private LayoutInflater itemLongClickedPopWindowInflater;
private View itemLongClickedPopWindowView;
private Context context;
private int type;
/** * 构造函数 * @param context 上下文 * @param width 宽度 * @param height 高度 * */
public ItemLongClickedPopWindow(Context context, int type, int width, int height) {
super(context);
this.context = context;
this.type = type;
//创建
this.initTab();
//设置默认选项
setWidth(width);
setHeight(height);
setContentView(this.itemLongClickedPopWindowView);
setOutsideTouchable(true);
setFocusable(true);
}
//实例化
private void initTab() {
this.itemLongClickedPopWindowInflater = LayoutInflater.from(this.context);
switch (type) {
// case FAVORITES_ITEM_POPUPWINDOW:
// this.itemLongClickedPopWindowView = this.itemLongClickedPopWindowInflater.inflate(R.layout.list_item_longclicked_favorites, null);
// break;
// case FAVORITES_VIEW_POPUPWINDOW: //对于书签内容弹出菜单,未作处理
// break;
// case HISTORY_ITEM_POPUPWINDOW:
// this.itemLongClickedPopWindowView = this.itemLongClickedPopWindowInflater.inflate(R.layout.list_item_longclicked_history, null);
// break;
// case HISTORY_VIEW_POPUPWINDOW: //对于历史内容弹出菜单,未作处理
// break;
// case ACHOR_VIEW_POPUPWINDOW: //超链接
// break;
case IMAGE_VIEW_POPUPWINDOW: //图片
this.itemLongClickedPopWindowView = this.itemLongClickedPopWindowInflater.inflate(R.layout.list_item_longclicked_img, null);
break;
}
}
public View getView(int id) {
return this.itemLongClickedPopWindowView.findViewById(id);
}
}
PopWindow的布局文件——list_item_longclicked_img.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp" android:orientation="vertical">
<TextView android:id="@+id/item_longclicked_viewImage" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="查看图片" android:textColor="@android:color/white" android:textSize="16sp" />
<View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/gray_9F" />
<TextView android:id="@+id/item_longclicked_saveImage" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="保存图片" android:textColor="@android:color/white" android:textSize="16sp" />
</LinearLayout>
通过GestureDetector获取按下的位置,来定位PopWindow显示的位置
private GestureDetector gestureDetector;
private int downX, downY;
gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
@Override
public void onLongPress(MotionEvent e) {
downX = (int) e.getX();
downY = (int) e.getY();
}
});
在Activity中对webview的监听
_mWebview.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
WebView.HitTestResult result = ((WebView)v).getHitTestResult();
if (null == result)
return false;
int type = result.getType();
if (type == WebView.HitTestResult.UNKNOWN_TYPE)
return false;
if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
//let TextViewhandles context menu return true;
}
final ItemLongClickedPopWindow itemLongClickedPopWindow = new ItemLongClickedPopWindow(HtmlActivity.this,ItemLongClickedPopWindow.IMAGE_VIEW_POPUPWINDOW, UIUtils.dip2px(120), UIUtils.dip2px(90));
// Setup custom handlingdepending on the type
switch (type) {
case WebView.HitTestResult.PHONE_TYPE: // 处理拨号
break;
case WebView.HitTestResult.EMAIL_TYPE: // 处理Email
break;
case WebView.HitTestResult.GEO_TYPE: // TODO
break;
case WebView.HitTestResult.SRC_ANCHOR_TYPE: // 超链接
// Log.d(DEG_TAG, "超链接");
break;
case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
break;
case WebView.HitTestResult.IMAGE_TYPE: // 处理长按图片的菜单项
imgurl = result.getExtra();
//通过GestureDetector获取按下的位置,来定位PopWindow显示的位置
itemLongClickedPopWindow.showAtLocation(v, Gravity.TOP|Gravity.LEFT, downX, downY + 10);
break;
default:
break;
}
itemLongClickedPopWindow.getView(R.id.item_longclicked_viewImage)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
itemLongClickedPopWindow.dismiss();
}
});
itemLongClickedPopWindow.getView(R.id.item_longclicked_saveImage)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
itemLongClickedPopWindow.dismiss();
new SaveImage().execute(); // Android 4.0以后要使用线程来访问网络
}
});
return true;
}
});
最后下载图片
private String imgurl = "";
/*** * 功能:用线程保存图片 * * @author wangyp */
private class SaveImage extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String result = "";
try {
String sdcard = Environment.getExternalStorageDirectory().toString();
File file = new File(sdcard + "/Download");
if (!file.exists()) {
file.mkdirs();
}
int idx = imgurl.lastIndexOf(".");
String ext = imgurl.substring(idx);
file = new File(sdcard + "/Download/" + new Date().getTime() + ext);
InputStream inputStream = null;
URL url = new URL(imgurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(20000);
if (conn.getResponseCode() == 200) {
inputStream = conn.getInputStream();
}
byte[] buffer = new byte[4096];
int len = 0;
FileOutputStream outStream = new FileOutputStream(file);
while ((len = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
outStream.close();
result = "图片已保存至:" + file.getAbsolutePath();
} catch (Exception e) {
result = "保存失败!" + e.getLocalizedMessage();
}
return result;
}
@Override
protected void onPostExecute(String result) {
showToast(result);
}
}
原文:http://www.educity.cn/wenda/179077.html