Rapid Subscribe Android App

Rapid Subscribe Android App
Rapid Subscriber

Recent Posts

Share URL image with android Web view | share image from android Web view to another app

In previous posts, we create a context menu and add two buttons of download image and copy image address.

Today, we create a Share Image button to share an image to another app.

For that, we add a new Picasso library in our app-level Gradle file.

Gradle dependency
 implementation 'com.squareup.picasso:picasso:2.71828'  

Then we add a new button in our context menu in our Main activity.java

MainActivity.java Context menu
   public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {  
     super.onCreateContextMenu(menu, v, menuInfo);  
     final WebView.HitTestResult webviewHittestResult = mywebview.getHitTestResult();  
     String DownloadImageUrl = webviewHittestResult.getExtra();  
     if(webviewHittestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||  
         webviewHittestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE)  
         menu.setHeaderTitle("Download Image from Below");  
         menu.add(0,1,0,"Share").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {  
           public boolean onMenuItemClick(MenuItem menuItem) {  
             Picasso.get().load(DownloadImageUrl).into(new Target() {  
               public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {  
                 Intent i = new Intent(Intent.ACTION_SEND);  
                 startActivity(Intent.createChooser(i,"Share Image"));  
               public void onBitmapFailed(Exception e, Drawable errorDrawable) {  
               public void onPrepareLoad(Drawable placeHolderDrawable) {  
             return false;  
       Toast.makeText(this, "Error Downloading", Toast.LENGTH_SHORT).show();  
   public Uri getloacalBitmaUri(Bitmap bmp)  
     Uri bmpuri = null;  
       File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),"shareimage"+ System.currentTimeMillis()+".png");  
       FileOutputStream out = new FileOutputStream(file);  
       bmpuri = FileProvider.getUriForFile(getApplicationContext(),"studio.harpreet.mybrowser.provider",file);  
     catch(IOException e)  
     return bmpuri;  

Here, we create a context menu which is shown when we long click on any image in our Webview. Then, we add a share image button in our context menu. Then we also add a new method for handling a Bitmap image. getloacalBitmaUri method is used to save the image in our pictures directory. Then we use Picasso to load that saved bitmap image and send it to another app.
Then we add a Provider tag in android manifest.


         android:resource="@xml/file_provider_path" />  
Then we add a new XML File which we called in this Provider Tag. named  file_provider_path

 <?xml version="1.0" encoding="utf-8"?>  
   xmlns:android = "http://schemas.android.com/apk/res/android">  
   <external-path name="image_capture" path="."/>  

Now, when we long click on the image which is shown in our WebView and click share image button then we successfully share that image to another app. 

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

No comments