如何在android WebView中启用longpress动作下载图像?

时间:2017-12-16 23:00:43

标签: javascript java android android-studio android-webview

我想在webview中对图像启用长按下载操作。这是我为此尝试的代码剪切:

 public void onCreate(Bundle savedInstanceState) {
        registerForContextMenu(descriptionWebView); 
    }

    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
      WebView.HitTestResult hitTestResult = descriptionWebView.getHitTestResult();
      switch (hitTestResult.getType()) {
        // ...
      }
    }

1 个答案:

答案 0 :(得分:1)

您需要注册WebView以获取上下文菜单。在yout activity registerForContextMenu(webView);

并覆盖后显示的onCreateContextMenu方法

WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webView = (WebView)findViewById(R.id.WebView1);

    webView.getSettings().setJavaScriptEnabled(true);

    webView.setWebViewClient(new WebViewClient());

    registerForContextMenu(webView);

    webView.loadUrl(HTTP_URL);
}

@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo){
    super.onCreateContextMenu(contextMenu, view, contextMenuInfo);

    final WebView.HitTestResult webViewHitTestResult = webView.getHitTestResult();

    if (webViewHitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
            webViewHitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {

        contextMenu.setHeaderTitle("Download Image From Below");

        contextMenu.add(0, 1, 0, "Save - Download Image")
                .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {

                        String DownloadImageURL = webViewHitTestResult.getExtra();

                        if(URLUtil.isValidUrl(DownloadImageURL)){

                            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadImageURL));
                            request.allowScanningByMediaScanner();
                            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                            DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                            downloadManager.enqueue(request);

                            Toast.makeText(MainActivity.this,"Image Downloaded Successfully.",Toast.LENGTH_LONG).show();
                        }
                        else {
                            Toast.makeText(MainActivity.this,"Sorry.. Something Went Wrong.",Toast.LENGTH_LONG).show();
                        }
                        return false;
                    }
                });
    }
}

或者这是另类

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    final WebView.HitTestResult result = browser.getHitTestResult();
    MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            // handle on context menu click
            return true;
        }
    };

    if (result.getType() == WebView.HitTestResult.IMAGE_TYPE ||
            result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {

        menu.setHeaderTitle(result.getExtra());
        menu.add(0, 666, 0, "Save Image").setOnMenuItemClickListener(handler);
    }
}