X
X
xaiponews2020-11-07 20:12:44
Android
xaiponews, 2020-11-07 20:12:44

How to open image from webview in new activity?

I have an application that displays a web page. It is necessary that when you click on the image, it opens in a new acivity. Subsequently, various manipulations will be performed there, such as scaling and moving the focus. What commands and methods to use?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Edward, 2020-11-07
@evsmusic

You can use JavaScriptInterface, write a simple script, by clicking on the desired image, the string with the url will be transferred back to the application, and then open the activity and do what you need with the image url.
https://developer.android.com/guide/webapps/webview...

X
xaiponews, 2020-11-17
@xaiponews

Here is the solution

@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        final WebView.HitTestResult webViewHitTestResult = webView.getHitTestResult();
        if (webViewHitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE || webViewHitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE){
            
            menu.add("Сохранить изображение").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {

                    String downloadImg = webViewHitTestResult.getExtra();
                    if (URLUtil.isValidUrl(downloadImg)){
                        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadImg));
                        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
                        request.setAllowedOverRoaming(false);
                        request.setTitle("GadgetSaint Downloading " + "Sample" + ".png");
                        request.setDescription("Downloading " + "Sample" + ".png");
                        request.setVisibleInDownloadsUi(true);
                        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/GadgetSaint/"  + "/" + "Sample" + ".png");
                        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION);
                        DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                        downloadManager.enqueue(request);
                    }
                    return false;
                }
            });
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question