在webVIEw加载的网页中选择文件可以正常调用原生的选择文件功能
webVIEw#setWebChromeClIEntpublic class MainActivity extends AppCompatActivity { private WebVIEw webVIEw; private ValueCallback<Uri> uploadfile; private ValueCallback<Uri[]> uploadfiles; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); webVIEw = findVIEwByID(R.ID.webvIEw); webVIEw.setWebChromeClIEnt(new WebChromeClIEnt() { // For AndroID 3.0+ public voID openfileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { Log.i("test", "openfileChooser 1"); uploadfile = uploadfile; openfileChooseProcess(); } // For AndroID < 3.0 public voID openfileChooser(ValueCallback<Uri> uploadMsgs) { Log.i("test", "openfileChooser 2"); uploadfile = uploadfile; openfileChooseProcess(); } // For AndroID > 4.1.1 public voID openfileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { Log.i("test", "openfileChooser 3"); uploadfile = uploadfile; openfileChooseProcess(); } // For AndroID >= 5.0 public boolean onShowfileChooser(WebVIEw webVIEw, ValueCallback<Uri[]> filePathCallback, WebChromeClIEnt.fileChooserParams fileChooserParams) { Log.i("test", "openfileChooser 4:" + filePathCallback.toString()); uploadfiles = filePathCallback; openfileChooseProcess(); return true; } }); webVIEw.loadUrl("file:///androID_asset/fileChooser.HTML"); } private voID openfileChooseProcess() { Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addcategory(Intent.category_OPENABLE); i.setType("*/*"); startActivityForResult(Intent.createChooser(i, "选择文件"), 0); } @OverrIDe protected voID onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESulT_OK) { switch (requestCode) { case 0: if (uploadfile != null) { Uri result = data == null || resultCode != RESulT_OK ? null : data.getData(); uploadfile.onReceiveValue(result); uploadfile = null; } if (uploadfiles != null) { Uri result = data == null || resultCode != RESulT_OK ? null : data.getData(); uploadfiles.onReceiveValue(new Uri[]{result}); uploadfiles = null; } break; default: break; } } else if (resultCode == RESulT_CANCELED) { if (uploadfile != null) { uploadfile.onReceiveValue(null); uploadfile = null; } if (uploadfiles != null) { uploadfiles.onReceiveValue(null); uploadfiles = null; } } } @OverrIDe protected voID onResume() { super.onResume(); webVIEw.onResume(); } @OverrIDe protected voID onPause() { super.onPause(); webVIEw.onPause(); } /** * 确保注销配置能够被释放 */ @OverrIDe protected voID onDestroy() { if (webVIEw != null) { webVIEw.destroy(); } super.onDestroy(); }}
demo下载
点赞收藏分享文章举报这个杀手不太累发布了135 篇原创文章 · 获赞 57 · 访问量 22万+私信 关注 总结以上是内存溢出为你收集整理的Android中WebView网页上传文件设置全部内容,希望文章能够帮你解决Android中WebView网页上传文件设置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)