如何从Web视图将图像保存到APP内部目录中?

时间:2018-08-01 07:22:52

标签: android android-webview

这段代码将允许我在移动设备的本地目录中下载图像,但是我无法将图像保存在APP目录中,因此我将路径Environment.DIRECTORY_DOWNLOADS更改为我的本地路径。

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,URLUtil.guessFileName(DownloadImageURL,“ ahsan”,“ image / png”));

//for downloading images from google browser
@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo){
    super.onCreateContextMenu(contextMenu, view, contextMenuInfo);

最终的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);

                            //  DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadImageURL));

                            request.setMimeType("image/png");
                            //------------------------COOKIE!!------------------------
                            String cookies = CookieManager.getInstance().getCookie(DownloadImageURL);
                            request.addRequestHeader("cookie", cookies);
                            //------------------------COOKIE!!------------------------
                            //     request.addRequestHeader("User-Agent", userAgent);
                            request.setDescription("Downloading file...");
                            request.setTitle(URLUtil.guessFileName(DownloadImageURL, "ahsan", "image/png"));
                            request.allowScanningByMediaScanner();
                            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);



                            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(DownloadImageURL, "ahsan", "image/png"));
                            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

                            dm.enqueue(request);
                            Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();

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

                    }
                });
    }

1 个答案:

答案 0 :(得分:1)

我在此处发布正确答案,以将图像从webview保存到应用程序内部存储目录中。为此,我使用的是完美的prodownloader库 只需添加图片网址,图片保存的路径以及带有我在下面提到的扩展名的图片名称。 寒意:)

  

implementation 'com.mindorks.android:prdownloader:0.4.0'



                  PRDownloader.download(image url, internal path where image will save , name of image with extension e.g .png)
                          .build()
                          .setOnProgressListener(new OnProgressListener()
                          {
                              @Override
                              public void onProgress(Progress progress)
                              {
                              }
                          })
                          .start(new OnDownloadListener()
                          {
                              @Override
                              public void onDownloadComplete()
                              {

                              }

                              @Override
                              public void onError(Error error)
                              {

                              }

                          });