原地址:http://blog.csdn.net/djcken/article/details/46379929

解决这个问题花了很长时间搜索了解,网上大部分使用openFileChooser但都没解决一个存在的问题。就是当弹出选择图片/相机框之后,取消选择,就再也不能点击选择按钮了。这篇文章是为了记录这一点,为验证整个流程部署了后端,但是由于很久没接触后端,后端代码是网上的列子,所以后端代码和部署就不说了。单纯的说下Android端的解决方案。

自定义两个文件:

  1. /**
  2. * 自定义
  3. *
  4. * @Author KenChung
  5. */
  6. public class ReWebViewClient extends WebViewClient {
  7. @Override
  8. public void onPageStarted(WebView view, String url, Bitmap favicon) {
  9. super.onPageStarted(view, url, favicon);
  10. }
  11. @Override
  12. public void onPageFinished(WebView view, String url) {
  13. super.onPageFinished(view, url);
  14. }
  15. }
  1. /**
  2. * ReWebChomeClient
  3. *
  4. * @Author KenChung
  5. */
  6. public class ReWebChomeClient extends WebChromeClient {
  7. private OpenFileChooserCallBack mOpenFileChooserCallBack;
  8. public ReWebChomeClient(OpenFileChooserCallBack openFileChooserCallBack) {
  9. mOpenFileChooserCallBack = openFileChooserCallBack;
  10. }
  11. //For Android 3.0+
  12. public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
  13. mOpenFileChooserCallBack.openFileChooserCallBack(uploadMsg, acceptType);
  14. }
  15. // For Android < 3.0
  16. public void openFileChooser(ValueCallback<Uri> uploadMsg) {
  17. openFileChooser(uploadMsg, "");
  18. }
  19. // For Android  > 4.1.1
  20. public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
  21. openFileChooser(uploadMsg, acceptType);
  22. }
  23. public interface OpenFileChooserCallBack {
  24. void openFileChooserCallBack(ValueCallback<Uri> uploadMsg, String acceptType);
  25. }
  26. }

选择图片弹框使用AlertDialog:

  1. public void showOptions() {
  2. AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
  3. alertDialog.setOnCancelListener(new ReOnCancelListener());
  4. alertDialog.setTitle(R.string.options);
  5. alertDialog.setItems(R.array.options, new DialogInterface.OnClickListener() {
  6. @Override
  7. public void onClick(DialogInterface dialog, int which) {
  8. if (which == 0) {
  9. mSourceIntent = ImageUtil.choosePicture();
  10. startActivityForResult(mSourceIntent, REQUEST_CODE_PICK_IMAGE);
  11. } else {
  12. mSourceIntent = ImageUtil.takeBigPicture();
  13. startActivityForResult(mSourceIntent, REQUEST_CODE_IMAGE_CAPTURE);
  14. }
  15. }
  16. }
  17. );
  18. alertDialog.show();
  19. }

关键代码:(这里的意思是取消弹框之后要告诉WebView不要再等待返回结果,设置为空就等于重置了状态)

  1. private class ReOnCancelListener implements DialogInterface.OnCancelListener {
  2. @Override
  3. public void onCancel(DialogInterface dialogInterface) {
  4. if (mUploadMsg != null) {
  5. mUploadMsg.onReceiveValue(null);
  6. mUploadMsg = null;
  7. }
  8. }
  9. }

完整MainActivity:

  1. /**
  2. * WebViewUpload
  3. *
  4. * @Author KenChung
  5. */
  6. public class MyActivity extends Activity implements ReWebChomeClient.OpenFileChooserCallBack {
  7. private static final String TAG = "MyActivity";
  8. private static final int REQUEST_CODE_PICK_IMAGE = 0;
  9. private static final int REQUEST_CODE_IMAGE_CAPTURE = 1;
  10. private WebView mWebView;
  11. private Intent mSourceIntent;
  12. private ValueCallback<Uri> mUploadMsg;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. mWebView = (WebView) findViewById(R.id.webview);
  18. mWebView.setWebChromeClient(new ReWebChomeClient(this));
  19. mWebView.setWebViewClient(new ReWebViewClient());
  20. fixDirPath();
  21. //这里加载自己部署的(也可加载本地资源)
  22. mWebView.loadUrl("file:///android_asset/input.html");
  23. }
  24. @Override
  25. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  26. if (resultCode != Activity.RESULT_OK) {
  27. return;
  28. }
  29. switch (requestCode) {
  30. case REQUEST_CODE_IMAGE_CAPTURE:
  31. case REQUEST_CODE_PICK_IMAGE: {
  32. try {
  33. if (mUploadMsg == null) {
  34. return;
  35. }
  36. String sourcePath = ImageUtil.retrievePath(this, mSourceIntent, data);
  37. if (TextUtils.isEmpty(sourcePath) || !new File(sourcePath).exists()) {
  38. Log.w(TAG, "sourcePath empty or not exists.");
  39. break;
  40. }
  41. Uri uri = Uri.fromFile(new File(sourcePath));
  42. mUploadMsg.onReceiveValue(uri);
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. break;
  47. }
  48. }
  49. }
  50. @Override
  51. public void openFileChooserCallBack(ValueCallback<Uri> uploadMsg, String acceptType) {
  52. mUploadMsg = uploadMsg;
  53. showOptions();
  54. }
  55. public void showOptions() {
  56. AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
  57. alertDialog.setOnCancelListener(new ReOnCancelListener());
  58. alertDialog.setTitle(R.string.options);
  59. alertDialog.setItems(R.array.options, new DialogInterface.OnClickListener() {
  60. @Override
  61. public void onClick(DialogInterface dialog, int which) {
  62. if (which == 0) {
  63. mSourceIntent = ImageUtil.choosePicture();
  64. startActivityForResult(mSourceIntent, REQUEST_CODE_PICK_IMAGE);
  65. } else {
  66. mSourceIntent = ImageUtil.takeBigPicture();
  67. startActivityForResult(mSourceIntent, REQUEST_CODE_IMAGE_CAPTURE);
  68. }
  69. }
  70. }
  71. );
  72. alertDialog.show();
  73. }
  74. private void fixDirPath() {
  75. String path = ImageUtil.getDirPath();
  76. File file = new File(path);
  77. if (!file.exists()) {
  78. file.mkdirs();
  79. }
  80. }
  81. private class ReOnCancelListener implements DialogInterface.OnCancelListener {
  82. @Override
  83. public void onCancel(DialogInterface dialogInterface) {
  84. if (mUploadMsg != null) {
  85. mUploadMsg.onReceiveValue(null);
  86. mUploadMsg = null;
  87. }
  88. }
  89. }
  90. }

有些哥们反馈没有附上html无法测试,放上html到本地即可:input.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="user-scalable=no">
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. </head>
  7. <body>
  8. <input id="input" type="file"/>
  9. </body>
  10. </html>

源码没有附上本地html,需自行创建。源码下载地址:CSDN下载

[转]Android使用WebView从相册/拍照中添加图片的更多相关文章

  1. 在RichTextBox控件中添加图片和文字

    public void SetText(RichTextBox rtb) { rtb.Text = "在RichTextBox控件中添加图片和文字" + Environment.N ...

  2. ArcMap图层属性表中添加图片

    一看标题是不是有点懵?懵就对了!刚接触到的时候我也有点懵,属性表不是都是文本啊数字啊之类的格式,怎么还可以存图片,下面就带大家来看看吧! 一.关于图层入库问题 图层进入数据库和图层以shp格式存储时, ...

  3. 关于在Silverlight中添加图片的问题

    在Silverlight中添加图片,目前支持的Image格式有jpg和png两种,如何在目录中添加,有些什么技巧呢? <StackPanel Background="White&quo ...

  4. 如何在github的README.md中添加图片

    如何在github的README.md中添加图片 总结: 链接引用:![Image text](图片的链接地址) 简介: 1.在github上的仓库建立一个存放图片的文件夹,文件夹名字随意.如:img ...

  5. ag-grid 表格中添加图片

    ag-grid是一种非常好用的表格,网上搜索会有各种各样的基本用法,不过对于在ag-grid 表格中添加图片我没有找到方法,看了官方的文档,当然英文的自己也是靠网页翻译,最后发现有这么一个例子,我知道 ...

  6. iview+vue 表格中添加图片

    开门见山,话不多说,要在表格中添加图片,可以使用td: <table " width="100%"> <tr class="tr-style ...

  7. 如何兼容所有Android版本选择照片或拍照然后裁剪图片--基于FileProvider和动态权限的实现

    我们知道, Android操作系统一直在进化. 虽然说系统是越来越安全, 可靠, 但是对于开发者而言, 开发难度是越来越大的, 需要注意的兼容性问题, 也越来越多. 就比如在Android平台上拍照或 ...

  8. [Android]ListView的Adapter.getView()方法中延迟加载图片的优化

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4139998.html 举个例子吧,以好友列表为例 ListVi ...

  9. Android 解决调用系统相册打不开图片 DecodeServices报解码错误

    这是由于系统相册不知道你图片目录是一个相册.打开前需要向系统相册“注册一下”,说白了就是让系统相册知道你这个图片所在的文件夹是个相册. private static void scanImageFil ...

随机推荐

  1. MySQL集群在断网后再启动报"Unable to start missing node group"问题处理

    总所周知,MySQL集群又名ndb cluster,而ndb就是network based database的简称,数据库节点之间依靠网络来通信和保证数据分块间的一致性.今天由于机房交换机损坏,导致集 ...

  2. 判断App运行是否在前台

    转自:http://notes.stay4it.com/2016/02/26/check-if-app-is-running-forground/ 在一些场景中,经常会需要判断App是否在后台运行,比 ...

  3. ghj

    如果对同一个元素的定义有多种,以最接近(最小一级)的定义为最优先,例如有这么一段代码 Update: Lorem ipsum dolor set 在CSS文件中,你已经定义了元素p,又定义了一个cla ...

  4. jquery .attr()

    在JS中设置节点的属性与属性值用到setAttribute(),获得节点的属性与属性值用到getAttribute(),而在jquery中,用一个attr()就可以全部搞定了,赞一个先 ^^ jque ...

  5. React(JSX语法)-----JSX属性

    1. if you know all the propertities that you want to place on a component ahead of time,it is easy t ...

  6. 写一些封装part1 (事件绑定移除,圆形矩形碰撞检测)

    var EventHandle = { addEvent:function(ele,type,handle){ if (ele.addEventListener) { ele.addEventList ...

  7. easyui datebox 设置不可编辑

    easyui datebox不允许编辑可以输入 editable="false"<input class="easyui-datebox" editabl ...

  8. python ABC

    因为项目需要,总是会有各种各样要重命名文件的场合,manual的方法当然不可取,bat的方法又感觉不够强大,所以就从零开始学python,就为了能够自动批量修改文件名,倒腾了一个周六,总算可以了 :) ...

  9. 【亚瑟士 ASICS 系列】

    [新配色 36-44] [亚瑟士 黑薄荷 大工厂流线 36-44] [亚瑟士 阿斯克斯 星空 水洗丹宁 36-44] [亚瑟士 阿斯克斯 经典爆 鼠尾草 36-44] [亚瑟士 ASICS Gel S ...

  10. C#子类调用基类构造备忘

    using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace First ...