Android WebView不支持H5输入type =“file”解决方法
2017-08-01 20:53
776 查看
<input style="width: 0px; height: 0px;" id="image" type="file" accept="image/*"> 1 1 上面的代码是H5里面的,我通过网页查看源码拿出来的,看了很多文章,我才知道,Android中webview却屏蔽了这个type =“file”这个功能,说是为了安全起见,真是搞不开始 我很迷茫,用手机可以打开网址,点击按钮可以打开相册,iOS也可以,尼玛,android就是没反应,有的机型甚至还崩溃,无语,苦闷,没办法,到群里去问,有个哥们很好,给了我点建议,和一些文章查看,终于搞定了。 说那么多,么有,上代码: private WebView wv; private String url ="http://shanghai.job1s.com/wap/member/index.php?c=photo"; private ValueCallback<Uri> mUploadMessage; public ValueCallback<Uri[]> uploadMessage; public static final int REQUEST_SELECT_FILE = 100; private final static int FILECHOOSER_RESULTCODE = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wv = (WebView) findViewById(R.id.webview); //设置支持Javascript wv.getSettings().setJavaScriptEnabled(true); wv.loadUrl(url); wv.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器 view.loadUrl(url); return false; } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { } @Override public void onPageFinished(WebView view, String url) { } }); wv.setWebChromeClient(new WebChromeClient(){ // For 3.0+ Devices (Start) // onActivityResult attached before constructor protected void openFileChooser(ValueCallback uploadMsg, String acceptType) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE); } // For Lollipop 5.0+ Devices @TargetApi(Build.VERSION_CODES.LOLLIPOP) public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { if (uploadMessage != null) { uploadMessage.onReceiveValue(null); uploadMessage = null; } uploadMessage = filePathCallback; Intent intent = fileChooserParams.createIntent(); try { startActivityForResult(intent, REQUEST_SELECT_FILE); } catch (ActivityNotFoundException e) { uploadMessage = null; Toast.makeText(getBaseContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show(); return false; } return true; } //For Android 4.1 only protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { mUploadMessage = uploadMsg; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE); } protected void openFileChooser(ValueCallback<Uri> 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); } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (requestCode == REQUEST_SELECT_FILE) { if (uploadMessage == null) return; uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent)); uploadMessage = null; } } else if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; // Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment // Use RESULT_OK only if you're implementing WebView inside an Activity Uri result = intent == null || resultCode != MainActivity.RESULT_OK ? null : intent.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage = null; } else Toast.makeText(getBaseContext(), "Failed to Upload Image", Toast.LENGTH_LONG).show(); }
相关文章推荐
- Android WebView 不支持 H5 input type="file" 解决方法
- Android WebView 不支持 H5 input type="file" 解决方法
- Android WebView 不支持 H5 input type="file" 解决方法
- Android WebView 不支持 H5 input type="file" 解决方法
- Android WebView H5页面 input type =“file”解决方法
- Android webview Input type=file 文件上传 解决方法
- 【Android进阶】Android里webview不支持input file的解决方法(上传按钮点击失效的原因)
- 【Android进阶】Android里webview不支持input file的解决方法(上传按钮点击失效的原因)
- Android WebView 支持H5图片上传<input type="file">
- android开发 webview支持type="file" 打开相机和相册
- Android笔记:Webview 支持 input type=file选择上传图片
- Android:让WebView支持<input type=”file”…>元素 - GreatK
- Android:让WebView支持<input type=”file”…>元素
- Android-WebView-解决对选择文件<input type="file">无响应
- 关于Android WebView不支持window.location.href的解决方法
- Android通过Chrome Inspect调试WebView的H5 App出现空白页面的解决方法(不需要FQ)
- 解决WebView不支持input type=file
- android Webview支持input type=file
- [置顶] 使用Android的webview将web app打包成安卓的app和解决安卓webview不支持input type=file问题
- android中webview 支持javascript alert不起作用的解决方法