Android: Let WebView support <input type= "file"... > Element - GreatK

Keywords: Android Google SDK Eclipse

Android: Let WebView support <input type="file"... > Element - GreatK

Time 2014-06-21 06:45:00 Blog Garden - All Essay Areas
Original text http://www.cnblogs.com/ilovewindy/p/3795111.html
Theme google android sdk

In Android, when we open a page through WebView, if there are elements in it that are <input type="file"... > Type, WebView can only display in normal style, but it can't be clicked. To solve this problem, we need to rewrite WebChromeClient.

The Demo code is given directly below:

Activity file:

public class MainActivity extends Activity {

	private final String host = "demo.com";
	private final String urlAddress = "http://" + host;

	private WebView web;
	private ProgressBar progressBar;

	private ValueCallback<Uri> mUploadMessage;
	private final static int FILECHOOSER_RESULTCODE = 1;

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
									Intent intent) {
		if (requestCode == FILECHOOSER_RESULTCODE) {
			if (null == mUploadMessage) return;
			Uri result = intent == null || resultCode != RESULT_OK ? null
					: intent.getData();
			mUploadMessage.onReceiveValue(result);
			mUploadMessage = null;
		}
	}

	/**
	 * Called when the activity is first created.
	 */
	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		web = (WebView) findViewById(R.id.webView1);
		progressBar = (ProgressBar) findViewById(R.id.progressBar1);

		WebSettings settings = web.getSettings();
		settings.setJavaScriptEnabled(true);
		web.loadUrl(urlAddress);
		web.setWebViewClient(new MyWebViewClient());

		web.setWebChromeClient(new WebChromeClient() {
			//Key code, the following functions are not API Documented, so in the ___________ Eclipse Chinese will report errors if added@Override Keyword here.

			// For Android 3.0+
			public void openFileChooser(ValueCallback<Uri> uploadMsg) {

				mUploadMessage = uploadMsg;
				Intent i = new Intent(Intent.ACTION_GET_CONTENT);
				i.addCategory(Intent.CATEGORY_OPENABLE);
				i.setType("image/*");
				MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);

			}

			// For Android 3.0+
			public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
				mUploadMessage = uploadMsg;
				Intent i = new Intent(Intent.ACTION_GET_CONTENT);
				i.addCategory(Intent.CATEGORY_OPENABLE);
				i.setType("*/*");
				MainActivity.this.startActivityForResult(
						Intent.createChooser(i, "File Browser"),
						FILECHOOSER_RESULTCODE);
			}

			//For Android 4.1
			public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
				mUploadMessage = uploadMsg;
				Intent i = new Intent(Intent.ACTION_GET_CONTENT);
				i.addCategory(Intent.CATEGORY_OPENABLE);
				i.setType("image/*");
				MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FILECHOOSER_RESULTCODE);

			}
		});

//		setContentView(web);
	}

	private class MyWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			if (Uri.parse(url).getHost().equals(host)) {
				// This is my web site, so do not override; let my WebView load the page
				return false;
			}
			// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
			Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
			startActivity(intent);
			return true;
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			// TODO Auto-generated method stub
			super.onPageStarted(view, url, favicon);
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			// TODO Auto-generated method stub
			super.onPageFinished(view, url);

			progressBar.setVisibility(View.GONE);
		}
	}


	//flipscreen not loading again
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	// Capture the "Back" button and let the WebView Can go back to the previous page instead of closing it directly Activity. 
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
			web.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}

Layout code is not posted, it is a very simple WebView and Progress. With the above code, we can upload files in WebView.

PS: Compared with iOS, this is not a perfect solution, because it does not support direct photo uploading. If this function is needed, further development is needed.

Posted by bruckerrlb on Fri, 08 Feb 2019 23:45:17 -0800