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
 @Override  
   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)  
     {  
       if(URLUtil.isNetworkUrl(DownloadImageUrl))  
       {  
         menu.setHeaderTitle("Download Image from Below");  
         menu.add(0,1,0,"Share").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {  
           @Override  
           public boolean onMenuItemClick(MenuItem menuItem) {  
             Picasso.get().load(DownloadImageUrl).into(new Target() {  
               @Override  
               public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {  
                 Intent i = new Intent(Intent.ACTION_SEND);  
                 i.setType("image/*");  
                 i.putExtra(Intent.EXTRA_STREAM,getloacalBitmaUri(bitmap));  
                 startActivity(Intent.createChooser(i,"Share Image"));  
               }  
               @Override  
               public void onBitmapFailed(Exception e, Drawable errorDrawable) {  
               }  
               @Override  
               public void onPrepareLoad(Drawable placeHolderDrawable) {  
               }  
             });  
             return false;  
           }  
         });  
       }  
     }  
     else  
     {  
       Toast.makeText(this, "Error Downloading", Toast.LENGTH_SHORT).show();  
     }  
   }  
   public Uri getloacalBitmaUri(Bitmap bmp)  
   {  
     Uri bmpuri = null;  
     try{  
       File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),"shareimage"+ System.currentTimeMillis()+".png");  
       FileOutputStream out = new FileOutputStream(file);  
       bmp.compress(Bitmap.CompressFormat.PNG,90,out);  
       out.close();  
       bmpuri = FileProvider.getUriForFile(getApplicationContext(),"studio.harpreet.mybrowser.provider",file);  
     }  
     catch(IOException e)  
     {  
       e.printStackTrace();  
     }  
     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.


AndroidManifest.xml


 <provider  
       android:name="androidx.core.content.FileProvider"  
       android:authorities="studio.harpreet.mybrowser.provider"  
       android:exported="false"  
       android:grantUriPermissions="true">  
       <meta-data  
         android:name="android.support.FILE_PROVIDER_PATHS"  
         android:resource="@xml/file_provider_path" />  
     </provider>  
Then we add a new XML File which we called in this Provider Tag. named  file_provider_path


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

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