H5调用手机的摄像头拍照上传以及手机相册选取照片

系统管理员 2022-09-25 12:19 368阅读 0赞

H5调用手机的摄像头拍照上传以及手机相册选取照片

语法:

  1. <input accept="audio/*|video/*|image/*|MIME_type">

提示:如需规定多个值,请使用逗号分隔(比如 )。

这里写图片描述

详情访问:w3c


使用步骤:

  1. <input accept="image/*" type="file">

目前支持情况是比较良好的,已在各大部分浏览器测试情况比较不错。

但是,如果H5嵌套在app上android机型就出问题了,该有的功能就无任何的效果了,为什么?

安卓端要看浏览器对这个标签内两个属性的优化,部分浏览器会直接跳转到资源管理器,优化做得好的可以直接使用。

H5嵌套在了android的webview中,处于安全考虑android webview 屏蔽了 input file ,所以,需要webview进行优化方可使用。我们用H5去调用上传文件,因此需要支持这项功能。

解决方案:

具体可以理解为 重写webview 的WebChromeClient方法。

  1. public class PlayActivity extends AppCompatActivity {
  2. @Override
  3. protected void onDestroy() {
  4. super.onDestroy();
  5. }
  6. /** * Called when the activity is first created. */
  7. WebView web;
  8. private ValueCallback<Uri> mUploadMessage;
  9. public ValueCallback<Uri[]> uploadMessage;
  10. public static final int REQUEST_SELECT_FILE = 100;
  11. private final static int FILECHOOSER_RESULTCODE = 1;
  12. @Override
  13. protected void onActivityResult(int requestCode, int resultCode,
  14. Intent intent) {
  15. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  16. if (requestCode == REQUEST_SELECT_FILE) {
  17. if (uploadMessage == null)
  18. return;
  19. uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
  20. uploadMessage = null;
  21. }
  22. } else if (requestCode == FILECHOOSER_RESULTCODE) {
  23. if (null == mUploadMessage)
  24. return;
  25. // Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment
  26. // Use RESULT_OK only if you're implementing WebView inside an Activity
  27. Uri result = intent == null || resultCode != MainActivity.RESULT_OK ? null : intent.getData();
  28. mUploadMessage.onReceiveValue(result);
  29. mUploadMessage = null;
  30. }
  31. }
  32. @Override
  33. public void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. setContentView(R.layout.activity_play);
  36. web = (WebView) findViewById(R.id.webview01);
  37. web = new WebView(this);
  38. web.getSettings().setJavaScriptEnabled(true);
  39. web.loadUrl("http://10.0.0.82:1346/hot/7516063/A1035B1ECD35AA7F0FAE050007F01000209");
  40. web.setWebViewClient(new myWebClient());
  41. web.setWebChromeClient(new WebChromeClient() {
  42. //The undocumented magic method override
  43. //Eclipse will swear at you if you try to put @Override here
  44. // For Android 3.0+
  45. public void openFileChooser(ValueCallback<Uri> uploadMsg) {
  46. mUploadMessage = uploadMsg;
  47. Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  48. i.addCategory(Intent.CATEGORY_OPENABLE);
  49. i.setType("image/*");
  50. PlayActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
  51. }
  52. public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
  53. openFileChooser(uploadMsg);
  54. }
  55. public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
  56. openFileChooser(uploadMsg);
  57. }
  58. // For Lollipop 5.0+ Devices
  59. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  60. public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
  61. if (uploadMessage != null) {
  62. uploadMessage.onReceiveValue(null);
  63. uploadMessage = null;
  64. }
  65. uploadMessage = filePathCallback;
  66. Intent intent = fileChooserParams.createIntent();
  67. try {
  68. startActivityForResult(intent, REQUEST_SELECT_FILE);
  69. } catch (ActivityNotFoundException e) {
  70. uploadMessage = null;
  71. return false;
  72. }
  73. return true;
  74. }
  75. });
  76. setContentView(web);
  77. }
  78. public class myWebClient extends WebViewClient {
  79. @Override
  80. public void onPageStarted(WebView view, String url, Bitmap favicon) {
  81. // TODO Auto-generated method stub
  82. super.onPageStarted(view, url, favicon);
  83. }
  84. @Override
  85. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  86. // TODO Auto-generated method stub
  87. view.loadUrl(url);
  88. return true;
  89. }
  90. @Override
  91. public void onPageFinished(WebView view, String url) {
  92. // TODO Auto-generated method stub
  93. super.onPageFinished(view, url);
  94. }
  95. }
  96. //flipscreen not loading again
  97. @Override
  98. public void onConfigurationChanged(Configuration newConfig) {
  99. super.onConfigurationChanged(newConfig);
  100. }
  101. }

详细,请移步这里


发表评论

表情:
评论列表 (有 0 条评论,368人围观)

还没有评论,来说两句吧...

相关阅读