Android Save Download Image from WebView on Long Press Image Tutorial

June 05, 2018 , , 0 Comments

Advance modern mobile application web browsers can give the facility to its users to Download Image instantly on Image Long Press event in WebView. So in this tutorial we would going to make a android application in android studio to Download-Save image in mobile phone gallery on Image Long Press.


Contents in this project Save Download Image from WebView on Long Press Image Tutorial :


1. Add Internet permission in AndroidManifest.xml file. Internet permission is very important because without the internet permission we cannot open any webpage in WebView.



2. Create WebView widget in activity_main.xml file.


    android:id="@+id/WebView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

3. Create WebView object and String URL in MainActivity.java file.


WebView webView;
String HTTP_URL = "https://www.google.com" ;

4. Assign ID to WebView.


webView = (WebView)findViewById(R.id.WebView1);

5. Now enable the JavaScript in WebView and setWebViewClient and finally load the String URL into WebView.


webView.getSettings().setJavaScriptEnabled(true);

webView.setWebViewClient(new WebViewClient());

registerForContextMenu(webView);

webView.loadUrl(HTTP_URL);

5. Now create onCreateContextMenu override method to create the Context menu in Alert box to download and save the image into Mobile phone gallery.


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

final WebView.HitTestResult webViewHitTestResult = webView.getHitTestResult();

if (webViewHitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
webViewHitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {

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

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

String DownloadImageURL = webViewHitTestResult.getExtra();

if(URLUtil.isValidUrl(DownloadImageURL)){

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

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

 Final Source Code :


Code for MainActivity.java file.


package com.android_examples.saveimagefromwebview_android_examplescom;
import android.app.DownloadManager;
import android.net.Uri;
import android.support.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.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

WebView webView;
String HTTP_URL = "https://www.google.com" ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webView = (WebView)findViewById(R.id.WebView1);

webView.getSettings().setJavaScriptEnabled(true);

webView.setWebViewClient(new WebViewClient());

registerForContextMenu(webView);

webView.loadUrl(HTTP_URL);
}

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

final WebView.HitTestResult webViewHitTestResult = webView.getHitTestResult();

if (webViewHitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
webViewHitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {

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

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

String DownloadImageURL = webViewHitTestResult.getExtra();

if(URLUtil.isValidUrl(DownloadImageURL)){

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

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

Code for activity_main.xml layout file.



xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.android_examples.saveimagefromwebview_android_examplescom.MainActivity">


android:id="@+id/WebView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>


Code for AndroidManifest.xml file.



package="com.android_examples.saveimagefromwebview_android_examplescom">



android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">










Screenshots:



Download Image




The post Android Save Download Image from WebView on Long Press Image Tutorial appeared first on Android Examples.

Unknown

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

0 comments:

Hot-product

Best Watch Store

Popular Posts

Contact Form

Name

Email *

Message *