Monday, January 20, 2020

Save image from WebView Android example

Save image from WebView Android example
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/base_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ecebff"
    >
    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = getApplicationContext();
        mActivity = MainActivity.this;
        mWebView = findViewById(R.id.web_view);
        // Enable javascript for web view
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new WebViewClient());
        // Register the context menu for web view
        registerForContextMenu(mWebView);
        String url = "https://www.google.com";
        mWebView.loadUrl(url);
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu,View v,ContextMenu.ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu, v, menuInfo);
        final WebView.HitTestResult result = mWebView.getHitTestResult();
        if (result.getType() == WebView.HitTestResult.IMAGE_TYPE ||
                result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
            menu.setHeaderTitle("CONTEXT MENU");
            menu.add(0, 1, 0, "Save Image")
                    .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    String imgUrl = result.getExtra();
                    if(URLUtil.isValidUrl(imgUrl)){
                        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(imgUrl));
                        request.allowScanningByMediaScanner();
                        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                        DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                        downloadManager.enqueue(request);
                        Toast.makeText(mContext,"image saved.",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(mContext,"Invalid image url.",Toast.LENGTH_SHORT).show();
                    }
                    return false;
                }
            });
        }
    }
}
Screenshots
Save image from WebView Android example