Share Coding

Tutorials, Problems, Stuffs …

Tag Archives: webview

Android WebView Upload Picture via Camera

When clicking an Input File button on webview,
we want to take a picture from camera and place the image to that input.

1. Declare instance variables

private final static int CAPTURE_RESULTCODE = 1;
private ValueCallback<Uri> mUploadMessage;
private String filePath;

2. Set custom webChromeClient to WebView

myWebView.setWebChromeClient(new WebChromeClient() {				
	@SuppressWarnings("unused")
	public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
		openFileChooser(uploadMsg);
	}

	@SuppressWarnings("unused")
	public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
		openFileChooser(uploadMsg);
	}

	public void openFileChooser(ValueCallback<Uri> uploadMsg) {
		this.mUploadMessage = uploadMsg;

		File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "appName");
		this.filePath = imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
		File file = new File(this.filePath);

		Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
		MainActivity.this.startActivityForResult(captureIntent,  CAPTURE_RESULTCODE); 
	}
});

3. Apply onActivityResult to handle the picture after capture

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
	if (requestCode == CAPTURE_RESULTCODE) {
		if (null == this.mUploadMessage || (resultCode != RESULT_OK && !new File(filePath).exists())) {
			this.mUploadMessage.onReceiveValue(null);
		} else {
			ContentValues values = new ContentValues();
			values.put(MediaStore.Images.Media.DATA, this.filePath);
			this.mUploadMessage.onReceiveValue(this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values));
		}
		this.mUploadMessage = null;
	}
}

Android WebView loadURL HTTPS BLANK

webView1 = (WebView) findViewById(R.id.webView1);
webView1.getSettings().setJavaScriptEnabled(true);
webView1.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView1.setWebViewClient(mWebClient);

private WebViewClient mWebClient = new WebViewClient() {
	@Override
	public boolean shouldOverrideUrlLoading(WebView view, String url) {
		view.loadUrl(url);
		return true;
	}

	public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){  
		handler.proceed();
	}  
};

Return value from JavaScript to Android Webview

WebView WebView1 = new WebView(this);
WebView1.getSettings().setJavaScriptEnabled(true);
WebView1.setWebChromeClient(MyWebChromeClient);
....
private WebChromeClient MyWebChromeClient = new WebChromeClient() {
	@Override
	public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
		try {
			// "message" is the text shown in alert box
			Log.v("message",java.net.URLDecoder.decode(message, "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return true;
	}
};
//If the "message" text is garbled, you can try to add this on php header: ini_set('default_charset','utf-8');

Programmatically disable Scroll function on Android WebView

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

//Only hide the scrollbar, not disables the scrolling:
WebView1.setVerticalScrollBarEnabled(false);
WebView1.setHorizontalScrollBarEnabled(false);

//Only disabled the horizontal scrolling:
WebView1.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

//To disabled the horizontal and vertical scrolling:
webview.setOnTouchListener(new View.OnTouchListener() {
	public boolean onTouch(View v, MotionEvent event) {
		return (event.getAction() == MotionEvent.ACTION_MOVE);
	}
});