启用Webview在长按上保存图像

时间:2015-01-29 11:11:21

标签: android webview

我正在尝试让我的webview带来一个选项,以便在长时间点击图像时保存当前图像我在oncreate之外尝试了以下代码,但它根本没有做任何事情!

 public boolean onLongClick(View v) {
     openContextMenu(v);
     return true;
 }
 @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
                     ContextMenu.ContextMenuInfo menuInfo) {
     super.onCreateContextMenu(menu, v, menuInfo);
     MenuInflater inflater = getMenuInflater();
     inflater.inflate(R.menu.context, menu);
 }
 @Override
 public boolean onContextItemSelected(MenuItem item) {
   AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
   switch (item.getItemId()) {
   case R.id.save_image:
       Toast.makeText(this, "save failed",
             Toast.LENGTH_LONG).show();
       return true;
   default:
     return super.onContextItemSelected(item);
   }
 }

1 个答案:

答案 0 :(得分:1)

@Override
protected void onCreateContextMenu(ContextMenu menu) {
    super.onCreateContextMenu(menu);

    HitTestResult result = getHitTestResult();

    MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
                // do the menu action
                return true;
        }
    };

    if (result.getType() == HitTestResult.IMAGE_TYPE ||
            result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
        // Menu options for an image.
        //set the header title to the image url
        menu.setHeaderTitle(result.getExtra());
        menu.add(0, ID_SAVEIMAGE, 0, "Save Image").setOnMenuItemClickListener(handler);
        menu.add(0, ID_VIEWIMAGE, 0, "View Image").setOnMenuItemClickListener(handler);
    }
}