三星、华为等android里webview不支持input file的解决方法
分类:软件问题| 发布:佚名| 查看:14776 | 发表时间:2014/5/16
由于安全因素android webview屏蔽了文件上传控件,但是他并没有完全封掉。
1 | < form method = "POST" enctype = "multipart/form-data" > |
2 | File to upload: < input type = "file" name = "uploadfile" > |
3 | < input type = "submit" value = "Press to Upload..." > to upload the file! |
1.activity定义
1 | public ValueCallback<Uri> mUploadMessage; |
2 | public final static int FILECHOOSER_RESULTCODE = 1 ; |
2.扩展WebChromeClient
1 | WebChromeClient chromeClient = new WebChromeClientImpl();view.setWebChromeClient(chromeClient); |
3.实现WebChromeClientImpl类
01 | private class WebChromeClientImpl extends WebChromeClient{ |
05 | public boolean onJsAlert(WebView view, String url, String message,JsResult result) { |
06 | Builder builder = new Builder(view.getContext()); |
07 | builder.setTitle( "商机通提示" ).setMessage(message).setPositiveButton( "确定" , null ); |
08 | builder.setCancelable( false ); |
09 | builder.setIcon(R.drawable.ic_launcher); |
10 | AlertDialog dialog = builder.create(); |
17 | public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { |
18 | mUploadMessage = uploadMsg; |
19 | Intent i = new Intent(Intent.ACTION_GET_CONTENT); |
20 | i.addCategory(Intent.CATEGORY_OPENABLE); |
22 | activity.startActivityForResult(Intent.createChooser(i, "File Chooser" ), FILECHOOSER_RESULTCODE); |
25 | public void openFileChooser(ValueCallback<Uri> uploadMsg) { |
26 | mUploadMessage = uploadMsg; |
27 | Intent i = new Intent(Intent.ACTION_GET_CONTENT); |
28 | i.addCategory(Intent.CATEGORY_OPENABLE); |
30 | activity.startActivityForResult(Intent.createChooser(i, "file Browser" ),FILECHOOSER_RESULTCODE); |
以下是所有的android版本的一个完整的解决方案
001 | public class MyWb extends Activity { |
002 | /** Called when the activity is first created. */ |
005 | ProgressBar progressBar; |
007 | private ValueCallback mUploadMessage; |
008 | private final static int FILECHOOSER_RESULTCODE= 1 ; |
011 | protected void onActivityResult( int requestCode, int resultCode, |
013 | if (requestCode==FILECHOOSER_RESULTCODE) |
015 | if ( null == mUploadMessage) return ; |
016 | Uri result = intent == null || resultCode != RESULT_OK ? null |
018 | mUploadMessage.onReceiveValue(result); |
019 | mUploadMessage = null ; |
024 | public void onCreate(Bundle savedInstanceState) { |
025 | super .onCreate(savedInstanceState); |
026 | setContentView(R.layout.main); |
028 | web = (WebView) findViewById(R.id.webview01); |
029 | progressBar = (ProgressBar) findViewById(R.id.progressBar1); |
031 | web = new WebView( this ); |
032 | web.getSettings().setJavaScriptEnabled( true ); |
034 | web.setWebViewClient( new myWebClient()); |
035 | web.setWebChromeClient( new WebChromeClient() |
040 | public void openFileChooser(ValueCallback uploadMsg) { |
042 | mUploadMessage = uploadMsg; |
043 | Intent i = new Intent(Intent.ACTION_GET_CONTENT); |
044 | i.addCategory(Intent.CATEGORY_OPENABLE); |
045 | i.setType( "image/*" ); |
046 | MyWb. this .startActivityForResult(Intent.createChooser(i, "File Chooser" ), FILECHOOSER_RESULTCODE); |
051 | public void openFileChooser( ValueCallback uploadMsg, String acceptType ) { |
052 | mUploadMessage = uploadMsg; |
053 | Intent i = new Intent(Intent.ACTION_GET_CONTENT); |
054 | i.addCategory(Intent.CATEGORY_OPENABLE); |
056 | MyWb. this .startActivityForResult( |
057 | Intent.createChooser(i, "File Browser" ), |
058 | FILECHOOSER_RESULTCODE); |
062 | public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture){ |
063 | mUploadMessage = uploadMsg; |
064 | Intent i = new Intent(Intent.ACTION_GET_CONTENT); |
065 | i.addCategory(Intent.CATEGORY_OPENABLE); |
066 | i.setType( "image/*" ); |
067 | MyWb. this .startActivityForResult( Intent.createChooser( i, "File Chooser" ), MyWb.FILECHOOSER_RESULTCODE ); |
079 | public class myWebClient extends WebViewClient |
082 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
084 | super .onPageStarted(view, url, favicon); |
088 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
097 | public void onPageFinished(WebView view, String url) { |
099 | super .onPageFinished(view, url); |
101 | progressBar.setVisibility(View.GONE); |
107 | public void onConfigurationChanged(Configuration newConfig){ |
108 | super .onConfigurationChanged(newConfig); |
113 | public boolean onKeyDown( int keyCode, KeyEvent event) |
115 | if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) { |
119 | return super .onKeyDown(keyCode, event); |
此外,我想补充一点, “上传页面”像在这个例子中, < 4个版本不会工作,因为它有一个图像预览功能,如果你想使它工作使用一个简单的php上传无预览。
对于一些手机品牌修改了android浏览器,比如:三星,我们可以查看他们官网找到解决办法的。samsung developers