Rapid Subscribe Android App

Rapid Subscribe Android App
Rapid Subscriber

Recent Posts

Download image button on Context menu with android WebView | Long click on android WebView Link



When we want to download an image from your android WebView, we need to set a context menu for that. When we long click on the image, it opens a short menu to ask what to do with that image.

In this post, we will download the image by opening the context menu on a long click on the image showing in our Web View.

To create a context menu, we use the override method called oncreatecontextmenu in our main activity

MainActivity.java
 @Override  
   public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {  
     super.onCreateContextMenu(menu, v, menuInfo);  
     final WebView.HitTestResult webviewHittestResult = mywebview.getHitTestResult();  
     DownloadImageUrl = webviewHittestResult.getExtra();  
     if(webviewHittestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||  
         webviewHittestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE)  
     {  
       if(URLUtil.isNetworkUrl(DownloadImageUrl))  
       {  
         menu.setHeaderTitle("Download Image from Below");  
         menu.add(0,1,0,"Download Image")  
             .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {  
               @Override  
               public boolean onMenuItemClick(MenuItem menuItem) {  
                 int Permission_all = 1;  
                 String Permission[] = {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};  
                 if(!hasPermission(MainActivity.this,Permission))  
                 {  
                   ActivityCompat.requestPermissions(MainActivity.this,Permission,Permission_all);  
                 }  
                 else  
                 {  
                   String filename = "";  
                   String type = null;  
                   String Mimetype = MimeTypeMap.getFileExtensionFromUrl(DownloadImageUrl);  
                   filename = URLUtil.guessFileName(DownloadImageUrl,DownloadImageUrl,Mimetype);  
                   if(Mimetype!=null)  
                   {  
                     type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(Mimetype);  
                   }  
                   if(type==null)  
                   {  
                     filename = filename.replace(filename.substring(filename.lastIndexOf(".")),".png");  
                     type = "image/*";  
                   }  
                   DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadImageUrl));  
                   request.allowScanningByMediaScanner();  
                   request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);  
                   request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,filename);  
                   DownloadManager manager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);  
                   manager.enqueue(request);  
                   Toast.makeText(MainActivity.this, "Check Notification", Toast.LENGTH_SHORT).show();  
                 }  
                 return false;  
               }  
             });  
       }  
     }  
     else  
     {  
       Toast.makeText(this, "Error Downloading", Toast.LENGTH_SHORT).show();  
     }  
   }  

public static boolean hasPermission(Context context, String... permissions)
    {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
                context!=null && permissions!=null)
        {
            for(String permission : permissions)
            {
                if(ActivityCompat.checkSelfPermission(context,permission) != PackageManager.PERMISSION_GRANTED)
                {
                    return false;
                }
            }
        }
        return true;
    }

Before that, create a String type variable call Downloadimageurl.

In this, we first check that if the link clicked is of image type then we perform our download manager and download that image.

After that, register your context menu in oncreate of that main activity

MainActivity.java oncreate method
 registerForContextMenu(mywebview);  




Follow us for more posts like this, 
Subscribe Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

No comments