【Android进阶】Android里webview不支持input file的解决方法(上传按钮点击失效的原因)

三星、华为等android里webview不支持input file的解决方法

原文地址:http://camnpr.com/archives/1093.html

由于安全因素android webview屏蔽了文件上传控件,但是他并没有完全封掉。

<form method="POST" enctype="multipart/form-data">
File to upload: <input type="file" name="uploadfile">  
<input type="submit" value="Press to Upload..."> to upload the file!
</form>


1.activity定义

private ValueCallback mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;


2.扩展WebChromeClient

WebChromeClient chromeClient = new WebChromeClientImpl();
webView.setWebChromeClient(chromeClient);

3.实现WebChromeClientImpl类

public class WebChromeClientImpl extends WebChromeClient {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
            builder.setTitle("提示").setMessage(message).setPositiveButton("确定", null);
            builder.setCancelable(false);
            AlertDialog dialog = builder.create();
            dialog.show();
            result.confirm();
            return true;
        }

        public void openFileChooser(ValueCallback uploadMsg) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
        }

        //扩展浏览器上传文件
        //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("*/*");
            startActivityForResult(
                    Intent.createChooser(i, "File Browser"),
                    FILECHOOSER_RESULTCODE);
        }

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

4.解决选择文件后按钮失效问题,需要覆写onActivityResult方法

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


下面是原文的一个完整案例,很有参考价值

public class MyWb extends Activity {
	/** Called when the activity is first created. */

	WebView web;
	ProgressBar progressBar;

	private ValueCallback 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;
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

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

		web = new WebView(this);
		web.getSettings().setJavaScriptEnabled(true);
		web.loadUrl("http://www.script-tutorials.com/demos/199/index.html");
		web.setWebViewClient(new myWebClient());
		web.setWebChromeClient(new WebChromeClient() {
			// The undocumented magic method override
			// Eclipse will swear at you if you try to put @Override here
			// For Android 3.0+
			public void openFileChooser(ValueCallback uploadMsg) {

				mUploadMessage = uploadMsg;
				Intent i = new Intent(Intent.ACTION_GET_CONTENT);
				i.addCategory(Intent.CATEGORY_OPENABLE);
				i.setType("image/*");
				MyWb.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("*/*");
				MyWb.this.startActivityForResult(
						Intent.createChooser(i, "File Browser"),
						FILECHOOSER_RESULTCODE);
			}

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

		});

		// 这句干啥的(⊙o⊙)?
		setContentView(web);

	}

	public class myWebClient extends WebViewClient

	{

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

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// TODO Auto-generated method stub
			view.loadUrl(url);
			return true;

		}

		@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);

	}

	// To handle "Back" key press event for WebView to go back to previous
	// screen.

	@Override
public boolean onKeyDown(int keyCode, KeyEvent event)

{

 if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {

 web.goBack();
 return true;

 }

 return super.onKeyDown(keyCode, event);

}
}

此外,我想补充一点, “上传页面”像在这个例子中, < 4个版本不会工作,因为它有一个图像预览功能,如果你想使它工作使用一个简单的php上传无预览。

对于一些手机品牌修改了android浏览器,比如:三星,我们可以查看他们官网找到解决办法的。samsung developers








  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值