Android Save Download Image from WebView on Long Press Image Tutorial

Advance trendy cellular software net browsers may give the power to its customers to Download Image immediately on Image Long Press occasion in NetView. So on this tutorial we might going to make a android software in android studio to Download-Save picture in cell phone gallery on Image Long Press.

Contents on this undertaking Save Download Image from NetView on Long Press Image Tutorial :

1. Add Internet permission in AndroidManifest.xml file. Internet permission is essential as a result of with out the web permission we can’t open any webpage in NetView.

2. Create NetView widget in activity_main.xml file.

three. Create NetView object and String URL in MainActivity.java file.

NetView netView;
String HTTP_URL = "https://www.google.com" ;

four. Assign ID to NetView.

netView = (NetView)discoverViewById(R.id.NetView1);

5. Now allow the JavaScript in NetView and setWebViewShopper and eventually load the String URL into NetView.

netView.getSettings().setJavaScriptEnabled(true);

netView.setWebViewShopper(new NetViewShopper());

registerForContextMenu(netView);

netView.loadUrl(HTTP_URL);

5. Now create onCreateContextMenu override technique to create the Context menu in Alert field to obtain and save the picture into Mobile telephone gallery.

@Override
    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo)
        tremendous.onCreateContextMenu(contextMenu, view, contextMenuInfo);

        remaining NetView.HitTestResult netViewHitTestResult = netView.getHitTestResult();

        if (netViewHitTestResult.getType() == NetView.HitTestResult.IMAGE_TYPE ||
                netViewHitTestResult.getType() == NetView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) 

            contextMenu.setHeaderTitle("Download Image From Below");

            contextMenu.add(zero, 1, zero, "Save - Download Image")
                    .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() 
                        @Override
                        public boolean onMenuItemClick(MenuItem menuItem) 

                            String DownloadImageURL = netViewHitTestResult.getExtra();

                            if(URLUtil.isValidUrl(DownloadImageURL))

                                DownloadSupervisor.Request request = new DownloadSupervisor.Request(Uri.parse(DownloadImageURL));
                                request.allowScanningByMediaScanner();
                                request.setNotificationVisibility(DownloadSupervisor.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                                DownloadSupervisor downloadManager = (DownloadSupervisor) getSystemService(DOWNLOAD_SERVICE);
                                downloadManager.enqueue(request);

                                Toast.makeText(MainActivity.this,"Image Downloaded Successfully.",Toast.LENGTH_LONG).present();
                            
                            else 
                                Toast.makeText(MainActivity.this,"Sorry.. Something Went Wrong.",Toast.LENGTH_LONG).present();
                            
                            return false;
                        
                    );
        
    

 Final Source Code :

Code for MainActivity.java file.

package deal com.android_examples.saveimagefromwebview_android_examplescom;
import android.app.DownloadSupervisor;
import android.internet.Uri;
import android.help.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.URLUtil;
import android.webkit.NetView;
import android.webkit.NetViewShopper;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity 

    NetView netView;
    String HTTP_URL = "https://www.google.com" ;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        tremendous.onCreate(savedInstanceState);
        setContentView(R.format.activity_main);

        netView = (NetView)discoverViewById(R.id.NetView1);

        netView.getSettings().setJavaScriptEnabled(true);

        netView.setWebViewShopper(new NetViewShopper());

        registerForContextMenu(netView);

        netView.loadUrl(HTTP_URL);
    

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo)
        tremendous.onCreateContextMenu(contextMenu, view, contextMenuInfo);

        ultimate NetView.HitTestResult netViewHitTestResult = netView.getHitTestResult();

        if (netViewHitTestResult.getType() == NetView.HitTestResult.IMAGE_TYPE ||
                netViewHitTestResult.getType() == NetView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) 

            contextMenu.setHeaderTitle("Download Image From Below");

            contextMenu.add(zero, 1, zero, "Save - Download Image")
                    .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() 
                        @Override
                        public boolean onMenuItemClick(MenuItem menuItem) 

                            String DownloadImageURL = netViewHitTestResult.getExtra();

                            if(URLUtil.isValidUrl(DownloadImageURL))

                                DownloadSupervisor.Request request = new DownloadSupervisor.Request(Uri.parse(DownloadImageURL));
                                request.allowScanningByMediaScanner();
                                request.setNotificationVisibility(DownloadSupervisor.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                                DownloadSupervisor downloadManager = (DownloadSupervisor) getSystemService(DOWNLOAD_SERVICE);
                                downloadManager.enqueue(request);

                                Toast.makeText(MainActivity.this,"Image Downloaded Successfully.",Toast.LENGTH_LONG).present();
                            
                            else 
                                Toast.makeText(MainActivity.this,"Sorry.. Something Went Wrong.",Toast.LENGTH_LONG).present();
                            
                            return false;
                        
                    );
        
    

Code for activity_main.xml format file.





    

Code for AndroidManifest.xml file.




    

    
        
            
                

                
            
        
    

Screenshots:

Download Image

The post Android Save Download Image from WebView on Long Press Image Tutorial appeared first on My APK Reviews.

Powered by WPeMatico