原地址: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. linux下JDK1.7安装

    http://mnt.conf.blog.163.com/blog/static/115668258201210793915876/ 一.软件下载1.下载JDK(下面分别是32位系统和64位系统下的版 ...

  2. ubuntu随笔

    在命令行里输入 sudo nautilus 之后输入你的用户的密码,会弹出一个目录窗口来,可以复制到这来

  3. Swiper 中文API手册(share)

    本文分享自 http://www.cnblogs.com/scavengers/p/3760449.html ---------------------------华丽的分割线------------ ...

  4. MFC下OpenGL入门(可以用)

    MFC下OpenGL入门 源文件 1, 建一工程文件,我这里命名为first,现在first工程里面我们没有添加任何东西,所有的东西都是MFC自动帮我们创建的. 2, 添加链接库.这一步很关键.打开菜 ...

  5. 如何制作快速加载的HTML页面

    整理出来的tip 减小页面的大小 在页面下载中,页面的大小至今扮演着非常重要的因素. 减小页面的大小能够通过排除不必要空格,注释,动态内嵌脚本,和放入外部文件的 CSS 等在页面结构中很小的改变都能够 ...

  6. js 九九乘法表

    /** * 第一行:1x1=1 * 第二行:2x1=2 2x2=4 * 第三行:3x1=3 3x2=6 3x3=9 */ document.write('<table border=" ...

  7. 今天我看了一个H5游戏EUI的例子,我都快分不清我到底是在用什么语言编译了代码了,作为刚刚学习H5游戏开发的菜鸟只能默默的收集知识

    今天看了一个EUI的demo,也是接触H5游戏开发的第五天了,我想看看我能不能做点什么出来,哎,自己写果然还是有问题的.在看EUI哪一个demo的时候就遇见了一些摇摆不定的问题,我觉得提出来 1.to ...

  8. Angularjs 服务注册

    $injector: (When you request a service, the $injector is responsible for finding the correct service ...

  9. const成员变量初始化总结

    const可以用来声明常量也就是说他的值不能被修改: const成员必须在定义的时候同时初始化,不能进行赋值 如 const int a:a的值不能修改,不能给它赋值,如何才能让它一开始就拥有一个值? ...

  10. [NOIP2015] 斗地主(搜索)

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...