Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context Menu for Image Saving in an Android webview

I've poured over similar questions but can't find the answer. I want to be able to long press the user-uploaded images in my webview to save them (like you can in a browser). Any help?

Update:

The context menu is popping up now with my own custom item "Save Image". I can even successfully toast msgs. How do I go about saving the image though? Is the image that's being long clicked being passed to my menu item?

        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);
          }
        }
like image 569
Andy Avatar asked Nov 01 '11 06:11

Andy


1 Answers

First register the WebView for context menus like so: activity.registerForContextMenu(webView)

@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.setHeaderTitle(result.getExtra());
        menu.add(0, ID_SAVEIMAGE, 0, "Save Image").setOnMenuItemClickListener(handler);
    }

To save you images simply use this

like image 60
Reno Avatar answered Sep 20 '22 03:09

Reno