package
com.androidexample.webview;
import
java.io.File;
import
android.app.Activity;
import
android.app.ProgressDialog;
import
android.content.Intent;
import
android.net.Uri;
import
android.os.Bundle;
import
android.os.Environment;
import
android.os.Parcelable;
import
android.provider.MediaStore;
import
android.webkit.ConsoleMessage;
import
android.webkit.ValueCallback;
import
android.webkit.WebChromeClient;
import
android.webkit.WebView;
import
android.webkit.WebViewClient;
import
android.webkit.WebSettings.PluginState;
import
android.widget.Toast;
public
class
ShowWebView
extends
Activity {
private
WebView webView;
final
Activity activity =
this
;
public
Uri imageUri;
private
static
final
int
FILECHOOSER_RESULTCODE =
2888
;
private
ValueCallback<Uri> mUploadMessage;
private
Uri mCapturedImageURI =
null
;
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.show_web_view);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(
true
);
webView.getSettings().setLoadWithOverviewMode(
true
);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(
false
);
webView.getSettings().setBuiltInZoomControls(
true
);
webView.getSettings().setPluginState(PluginState.ON);
webView.getSettings().setAllowFileAccess(
true
);
webView.getSettings().setSupportZoom(
true
);
webView.loadUrl(webViewUrl);
startWebView();
}
private
void
startWebView() {
webView.setWebViewClient(
new
WebViewClient() {
ProgressDialog progressDialog;
public
boolean
shouldOverrideUrlLoading(WebView view, String url) {
if
(url.contains(
"google"
)){
view.getContext().startActivity(
new
Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return
true
;
}
else
{
view.loadUrl(url);
return
true
;
}
}
public
void
onLoadResource (WebView view, String url) {
if
(progressDialog ==
null
&& url.contains(
"androidexample"
)
) {
progressDialog =
new
ProgressDialog(ShowWebView.
this
);
progressDialog.setMessage(
"Loading..."
);
progressDialog.show();
}
}
public
void
onPageFinished(WebView view, String url) {
try
{
if
(progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog =
null
;
}
}
catch
(Exception exception){
exception.printStackTrace();
}
}
});
webView.setWebChromeClient(
new
WebChromeClient() {
public
void
openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){
mUploadMessage = uploadMsg;
try
{
File imageStorageDir =
new
File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
,
"AndroidExampleFolder"
);
if
(!imageStorageDir.exists()) {
imageStorageDir.mkdirs();
}
File file =
new
File(
imageStorageDir + File.separator +
"IMG_"
+ String.valueOf(System.currentTimeMillis())
+
".jpg"
);
mCapturedImageURI = Uri.fromFile(file);
final
Intent captureIntent =
new
Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
Intent i =
new
Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType(
"image/*"
);
Intent chooserIntent = Intent.createChooser(i,
"Image Chooser"
);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS
,
new
Parcelable[] { captureIntent });
startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
}
catch
(Exception e){
Toast.makeText(getBaseContext(),
"Exception:"
+e,
Toast.LENGTH_LONG).show();
}
}
public
void
openFileChooser(ValueCallback<Uri> uploadMsg){
openFileChooser(uploadMsg,
""
);
}
public
void
openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType,
String capture) {
openFileChooser(uploadMsg, acceptType);
}
public
boolean
onConsoleMessage(ConsoleMessage cm) {
onConsoleMessage(cm.message(), cm.lineNumber(), cm.sourceId());
return
true
;
}
public
void
onConsoleMessage(String message,
int
lineNumber, String sourceID) {
}
});
}
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode,
Intent intent) {
if
(requestCode==FILECHOOSER_RESULTCODE)
{
if
(
null
==
this
.mUploadMessage) {
return
;
}
Uri result=
null
;
try
{
if
(resultCode != RESULT_OK) {
result =
null
;
}
else
{
result = intent ==
null
? mCapturedImageURI : intent.getData();
}
}
catch
(Exception e)
{
Toast.makeText(getApplicationContext(),
"activity :"
+e,
Toast.LENGTH_LONG).show();
}
mUploadMessage.onReceiveValue(result);
mUploadMessage =
null
;
}
}
@Override
public
void
onBackPressed() {
if
(webView.canGoBack()) {
webView.goBack();
}
else
{
super
.onBackPressed();
}
}
}