Android webview embedded html5 location File Upload
Simply add the code without saying anything.
Activity
Package com. xz. testwebview; import android. app. activity; import android. app. alertDialog; import android. content. context; import android. content. dialogInterface; import android. content. dialogInterface. onClickListener; import android. content. intent; import android. content. res. configuration; import android.net. uri; import android. OS. bundle; import android. view. keyEvent; import android. view. window; import android. view. windowManager; import android. webkit. valueCallback; import android. webkit. webChromeClient; import android. webkit. webSettings; import android. webkit. webSettings. layoutAlgorithm; import android. webkit. webView; import android. webkit. webViewClient;/***** @ author 3hxz **/public class MainActivity extends Activity {private WebView myWebView; private final static int metadata = 1; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); this. requestWindowFeature (Window. FEATURE_NO_TITLE); // remove the title bar this. getWindow (). setFlags (WindowManager. layoutParams. FLAG_FULLSCREEN, WindowManager. layoutParams. FLAG_FULLSCREEN); // remove the Information bar setContentView (R. layout. activity_main); // obtain the webview control myWebView = (WebView) findViewById (R. id. webview); // load the myWebView page on the server. loadUrl (Contants. url); // or load the local html // myWebView. loadUrl (file: // android_asset/www/test2.html); // Add the following code to make the link in the webpage not open myWebView in the browser. setWebViewClient (new WebViewClient (); // bind WebAppInterface to myWebView in javascript. addJavascriptInterface (new WebAppInterface (this), Android); // select the local photo file myWebView. setWebChromeClient (new WebChromeClient () {// For Android 4.1 (currently) public void openFileChooser (ValueCallback
UploadMsg, String acceptType, String capture) {Intent I = new Intent (Intent. ACTION_GET_CONTENT); I. addCategory (Intent. CATEGORY_OPENABLE); // view type String IMAGE_UNSPECIFIED = image/*; I. setType (image/*); MainActivity. this. startActivityForResult (Intent. createChooser (I, File Chooser), MainActivity. FILECHOOSER_RESULTCODE);} @ Override public void onGeolocationPermissionsHidePrompt () {super. onGeolocationPermiss IonsHidePrompt ();} // configure the permission @ Override public void onGeolocationPermissionsShowPrompt (final String origin, final android. webkit. geolocationPermissions. callback callback) {AlertDialog. builder builder = new AlertDialog. builder (MainActivity. this); builder. setMessage (whether to allow access to your geographic location information ?); OnClickListener dialogButtonOnClickListener = new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int clickedButton) {if (DialogInterface. BUTTON_POSITIVE = clickedButton) {callback. invoke (origin, true, true);} else if (DialogInterface. BUTTON_NEGATIVE = clickedButton) {callback. invoke (origin, false, false) ;}}; builder. setPositiveButton (allow, dialogButtonOnClickListener); builder. setNegativeButton (cancel, dialogButtonOnClickListener); builder. show (); super. onGeolocationPermissionsShowPrompt (origin, callback) ;}}); // obtain webview settings 336454 WebSettings webSettings = myWebView. getSettings (); // allow the use of javascriptwebSettings. setJavaScriptEnabled (true); // adaptive screen webSettings. setLayoutAlgorithm (LayoutAlgorithm. SINGLE_COLUMN); webSettings. setLoadWithOverviewMode (true); // You can scale webSettings. setsuppzoom zoom (true); // you can specify the Zoom tool webSettings. setBuiltInZoomControls (true); // scale up or down the webSettings. setUseWideViewPort (true); // sets the available webSettings for the database. setDatabaseEnabled (true); // obtain the address String dir = this. getApplicationContext (). getDir (database, Context. MODE_PRIVATE ). getPath (); webSettings. setDatabasePath (dir); // webSettings must be enabled when localStorage is used. setDomStorageEnabled (true); // enable geographic location webSettings. setGeolocationEnabled (true);}/*** responds with a key. When you view a webpage in WebView, return the page by browsing history when you press the return key, if this operation is not performed, the entire WebView will return exit */@ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK) & myWebView. canGoBack () {// The Return key is returned to myWebView. goBack (); return true;} return super. onKeyDown (keyCode, event );}}
WebAppInterface
public class WebAppInterface { Context mContext; /** Instantiate the interface and set the context */ WebAppInterface(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(); } }
AndroidManifest. xml
S
Activity_main.xml