Downloading images with webview

前端 未结 4 1418
北恋
北恋 2021-02-11 03:46

I m displaying a gallery from a mobile website in webview . How can i download those images from webview ? Are there any extra settings for webview ?

4条回答
  •  耶瑟儿~
    2021-02-11 04:37

    This solved my problem

    `          `@Override
                public boolean shouldOverrideUrlLoading (WebView view, String url) {
                    boolean shouldOverride = false;
                    // We only want to handle requests for image files, everything else the webview
                    // can handle normally
                    if (url.endsWith(".jpg")) {
                        shouldOverride = true;
                        Uri source = Uri.parse(url);
                        // Make a new request pointing to the mp3 url
                        DownloadManager.Request request = new DownloadManager.Request(source);
                        // Use the same file name for the destination
                        File destinationFile = new File (destinationDir, source.getLastPathSegment());
                        request.setDestinationUri(Uri.fromFile(destinationFile));
                        // Add it to the manager
                        manager.enqueue(request);
                    }
                    return shouldOverride;
                }``
    

    make sure to add permissions for download manager, SD read, SD write!

提交回复
热议问题