title: How to Download and save image in external storage from webview?
body: <ol>
  <li>I want to download images in sdCard from webview by long pressing on the images.</li>
  <li><p>I want to share image by long pressing on the image without storing the image in sdCard just like chrome.
  I can download images but they are not saving in sdCard.
  I almost searched on entire web but didn't get any solution.</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  WebView webView;
  String HTTP_URL = "https://www.google.com" ;
  private long downloadID ;
  Bitmap bitmap;
  Tag TAG;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  webView = (WebView)findViewById(R.id.myWebView);
  webView.getSettings().setJavaScriptEnabled(true);
  webView.setWebViewClient(new WebViewClient());
  webView.getSettings().setDomStorageEnabled(true);
  
  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("Select option");
  
  
  contextMenu.add(0, 1, 0, "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);
  downloadID = downloadManager.enqueue(request);
  String publicDirPath = ExternalStorageUtil.getPublicExternalStorageBaseDir("/");
  File newFile = new File(publicDirPath, "download");
  newFile.mkdirs();
  
  File newFile1 = new File(newFile, "image.png");
  OutputStream os;
  
  try {
  os = new FileOutputStream(newFile1);
  bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
  os.flush();
  os.close();
  } catch (FileNotFoundException e) {
  e.printStackTrace();
  } catch (IOException e) {
  e.printStackTrace();
  }
  
  } return false;
  }
  
  
  
  //set Download Listener
  
  webView.setDownloadListener(new DownloadListener() {
  
  public void onDownloadStart(String url, String userAgent,
  String contentDisposition, String mimetype,
  long contentLength) {
  
  DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
  request.allowScanningByMediaScanner();
  
  request.setNotificationVisibility(
  DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
  
  request.setDestinationInExternalPublicDir("timestop", "Tillem");
  
  DownloadManager dm = (DownloadManager) getSystemService(
  DOWNLOAD_SERVICE);
  
  dm.enqueue(request);
  
  
  }});
  }}
  </code></pre></li>
  </ol>