create this file and place it in your assets folder: webdemo.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>title</title>
</head>
<body>
<h1>Web View Demo</h1>
<br>
<input type="button" value="Say hello"
onClick="showAndroidToast('Hello Android!')" />
<br />
File Uri: <label id="lbluri">no file uri</label>
<br />
File Path: <label id="lblpath">no file path</label>
<br />
<input type="button" value="Choose Photo" onClick="choosePhoto()" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
function setFilePath(file) {
document.getElementById('lblpath').innerHTML = file;
Android.showToast(file);
}
function setFileUri(uri) {
document.getElementById('lbluri').innerHTML = uri;
Android.showToast(uri);
}
function choosePhoto() {
var file = Android.choosePhoto();
window.alert("file = " + file);
}
</script>
</body>
</html>
concentrate on the javascript written here.
write your activity as below:
WebViewDemo.java
public class WebViewDemo extends Activity
{
private WebView webView;
final int SELECT_PHOTO = 1;
@SuppressLint("SetJavaScriptEnabled")
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);
// Other webview settings
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.addJavascriptInterface(new MyJavascriptInterface(this), "Android");
webView.loadUrl("file:///android_asset/webdemo.html");
}
class MyJavascriptInterface
{
Context mContext;
/** Instantiate the interface and set the context */
MyJavascriptInterface(Context c)
{
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast)
{
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
@JavascriptInterface
public String choosePhoto()
{
// TODO Auto-generated method stub
String file = "test";
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
return file;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
switch (requestCode)
{
case SELECT_PHOTO:
if (resultCode == RESULT_OK)
{
Uri selectedImage = intent.getData();
webView.loadUrl("javascript:setFileUri('" + selectedImage.toString() + "')");
String path = getRealPathFromURI(this, selectedImage);
webView.loadUrl("javascript:setFilePath('" + path + "')");
}
}
}
public String getRealPathFromURI(Context context, Uri contentUri)
{
Cursor cursor = null;
try
{
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
finally
{
if (cursor != null)
{
cursor.close();
}
}
}
}
use this code snippet and see if it works.
this screenshot below is taken from a Android 4.4 Kitkat device.
I dont know if this solution works you or not. It may be just a workaround or a breakthrough for you.
I hope some part of it would help.