Ahsan Malik
Ahsan Malik

Reputation: 425

how Save Image in to APP INTERNAL DIRECTORY from web view?

This piece of code will allow me to download image in local dirctory of mobile but i am unable to save image in APP DIRECTORY i change the path Environment.DIRECTORY_DOWNLOADS to my local path .

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);

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);

                            //  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;

                    }
                });
    }

Upvotes: 0

Views: 135

Answers (1)

Ahsan Malik
Ahsan Malik

Reputation: 425

i am posting the correct answer here to save the image from webview into app internal storage directory . for this i use prodownloader library which is working perfectly just add image url, path where image will save and name of the image with extension i mentioned below. chill :)

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)
                              {

                              }

                          });

Upvotes: 1

Related Questions