[转]Android使用WebView从相册/拍照中添加图片
原地址:http://blog.csdn.net/djcken/article/details/46379929
解决这个问题花了很长时间搜索了解,网上大部分使用openFileChooser但都没解决一个存在的问题。就是当弹出选择图片/相机框之后,取消选择,就再也不能点击选择按钮了。这篇文章是为了记录这一点,为验证整个流程部署了后端,但是由于很久没接触后端,后端代码是网上的列子,所以后端代码和部署就不说了。单纯的说下Android端的解决方案。
自定义两个文件:
- /**
- * 自定义
- *
- * @Author KenChung
- */
- public class ReWebViewClient extends WebViewClient {
- @Override
- public void onPageStarted(WebView view, String url, Bitmap favicon) {
- super.onPageStarted(view, url, favicon);
- }
- @Override
- public void onPageFinished(WebView view, String url) {
- super.onPageFinished(view, url);
- }
- }
- /**
- * ReWebChomeClient
- *
- * @Author KenChung
- */
- public class ReWebChomeClient extends WebChromeClient {
- private OpenFileChooserCallBack mOpenFileChooserCallBack;
- public ReWebChomeClient(OpenFileChooserCallBack openFileChooserCallBack) {
- mOpenFileChooserCallBack = openFileChooserCallBack;
- }
- //For Android 3.0+
- public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
- mOpenFileChooserCallBack.openFileChooserCallBack(uploadMsg, acceptType);
- }
- // For Android < 3.0
- public void openFileChooser(ValueCallback<Uri> uploadMsg) {
- openFileChooser(uploadMsg, "");
- }
- // For Android > 4.1.1
- public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
- openFileChooser(uploadMsg, acceptType);
- }
- public interface OpenFileChooserCallBack {
- void openFileChooserCallBack(ValueCallback<Uri> uploadMsg, String acceptType);
- }
- }
选择图片弹框使用AlertDialog:
- public void showOptions() {
- AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
- alertDialog.setOnCancelListener(new ReOnCancelListener());
- alertDialog.setTitle(R.string.options);
- alertDialog.setItems(R.array.options, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- if (which == 0) {
- mSourceIntent = ImageUtil.choosePicture();
- startActivityForResult(mSourceIntent, REQUEST_CODE_PICK_IMAGE);
- } else {
- mSourceIntent = ImageUtil.takeBigPicture();
- startActivityForResult(mSourceIntent, REQUEST_CODE_IMAGE_CAPTURE);
- }
- }
- }
- );
- alertDialog.show();
- }
关键代码:(这里的意思是取消弹框之后要告诉WebView不要再等待返回结果,设置为空就等于重置了状态)
- private class ReOnCancelListener implements DialogInterface.OnCancelListener {
- @Override
- public void onCancel(DialogInterface dialogInterface) {
- if (mUploadMsg != null) {
- mUploadMsg.onReceiveValue(null);
- mUploadMsg = null;
- }
- }
- }
完整MainActivity:
- /**
- * WebViewUpload
- *
- * @Author KenChung
- */
- public class MyActivity extends Activity implements ReWebChomeClient.OpenFileChooserCallBack {
- private static final String TAG = "MyActivity";
- private static final int REQUEST_CODE_PICK_IMAGE = 0;
- private static final int REQUEST_CODE_IMAGE_CAPTURE = 1;
- private WebView mWebView;
- private Intent mSourceIntent;
- private ValueCallback<Uri> mUploadMsg;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mWebView = (WebView) findViewById(R.id.webview);
- mWebView.setWebChromeClient(new ReWebChomeClient(this));
- mWebView.setWebViewClient(new ReWebViewClient());
- fixDirPath();
- //这里加载自己部署的(也可加载本地资源)
- mWebView.loadUrl("file:///android_asset/input.html");
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (resultCode != Activity.RESULT_OK) {
- return;
- }
- switch (requestCode) {
- case REQUEST_CODE_IMAGE_CAPTURE:
- case REQUEST_CODE_PICK_IMAGE: {
- try {
- if (mUploadMsg == null) {
- return;
- }
- String sourcePath = ImageUtil.retrievePath(this, mSourceIntent, data);
- if (TextUtils.isEmpty(sourcePath) || !new File(sourcePath).exists()) {
- Log.w(TAG, "sourcePath empty or not exists.");
- break;
- }
- Uri uri = Uri.fromFile(new File(sourcePath));
- mUploadMsg.onReceiveValue(uri);
- } catch (Exception e) {
- e.printStackTrace();
- }
- break;
- }
- }
- }
- @Override
- public void openFileChooserCallBack(ValueCallback<Uri> uploadMsg, String acceptType) {
- mUploadMsg = uploadMsg;
- showOptions();
- }
- public void showOptions() {
- AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
- alertDialog.setOnCancelListener(new ReOnCancelListener());
- alertDialog.setTitle(R.string.options);
- alertDialog.setItems(R.array.options, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- if (which == 0) {
- mSourceIntent = ImageUtil.choosePicture();
- startActivityForResult(mSourceIntent, REQUEST_CODE_PICK_IMAGE);
- } else {
- mSourceIntent = ImageUtil.takeBigPicture();
- startActivityForResult(mSourceIntent, REQUEST_CODE_IMAGE_CAPTURE);
- }
- }
- }
- );
- alertDialog.show();
- }
- private void fixDirPath() {
- String path = ImageUtil.getDirPath();
- File file = new File(path);
- if (!file.exists()) {
- file.mkdirs();
- }
- }
- private class ReOnCancelListener implements DialogInterface.OnCancelListener {
- @Override
- public void onCancel(DialogInterface dialogInterface) {
- if (mUploadMsg != null) {
- mUploadMsg.onReceiveValue(null);
- mUploadMsg = null;
- }
- }
- }
- }
有些哥们反馈没有附上html无法测试,放上html到本地即可:input.html
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="user-scalable=no">
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- </head>
- <body>
- <input id="input" type="file"/>
- </body>
- </html>
源码没有附上本地html,需自行创建。源码下载地址:CSDN下载
[转]Android使用WebView从相册/拍照中添加图片的更多相关文章
- 在RichTextBox控件中添加图片和文字
public void SetText(RichTextBox rtb) { rtb.Text = "在RichTextBox控件中添加图片和文字" + Environment.N ...
- ArcMap图层属性表中添加图片
一看标题是不是有点懵?懵就对了!刚接触到的时候我也有点懵,属性表不是都是文本啊数字啊之类的格式,怎么还可以存图片,下面就带大家来看看吧! 一.关于图层入库问题 图层进入数据库和图层以shp格式存储时, ...
- 关于在Silverlight中添加图片的问题
在Silverlight中添加图片,目前支持的Image格式有jpg和png两种,如何在目录中添加,有些什么技巧呢? <StackPanel Background="White&quo ...
- 如何在github的README.md中添加图片
如何在github的README.md中添加图片 总结: 链接引用: 简介: 1.在github上的仓库建立一个存放图片的文件夹,文件夹名字随意.如:img ...
- ag-grid 表格中添加图片
ag-grid是一种非常好用的表格,网上搜索会有各种各样的基本用法,不过对于在ag-grid 表格中添加图片我没有找到方法,看了官方的文档,当然英文的自己也是靠网页翻译,最后发现有这么一个例子,我知道 ...
- iview+vue 表格中添加图片
开门见山,话不多说,要在表格中添加图片,可以使用td: <table " width="100%"> <tr class="tr-style ...
- 如何兼容所有Android版本选择照片或拍照然后裁剪图片--基于FileProvider和动态权限的实现
我们知道, Android操作系统一直在进化. 虽然说系统是越来越安全, 可靠, 但是对于开发者而言, 开发难度是越来越大的, 需要注意的兼容性问题, 也越来越多. 就比如在Android平台上拍照或 ...
- [Android]ListView的Adapter.getView()方法中延迟加载图片的优化
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4139998.html 举个例子吧,以好友列表为例 ListVi ...
- Android 解决调用系统相册打不开图片 DecodeServices报解码错误
这是由于系统相册不知道你图片目录是一个相册.打开前需要向系统相册“注册一下”,说白了就是让系统相册知道你这个图片所在的文件夹是个相册. private static void scanImageFil ...
随机推荐
- linux下JDK1.7安装
http://mnt.conf.blog.163.com/blog/static/115668258201210793915876/ 一.软件下载1.下载JDK(下面分别是32位系统和64位系统下的版 ...
- ubuntu随笔
在命令行里输入 sudo nautilus 之后输入你的用户的密码,会弹出一个目录窗口来,可以复制到这来
- Swiper 中文API手册(share)
本文分享自 http://www.cnblogs.com/scavengers/p/3760449.html ---------------------------华丽的分割线------------ ...
- MFC下OpenGL入门(可以用)
MFC下OpenGL入门 源文件 1, 建一工程文件,我这里命名为first,现在first工程里面我们没有添加任何东西,所有的东西都是MFC自动帮我们创建的. 2, 添加链接库.这一步很关键.打开菜 ...
- 如何制作快速加载的HTML页面
整理出来的tip 减小页面的大小 在页面下载中,页面的大小至今扮演着非常重要的因素. 减小页面的大小能够通过排除不必要空格,注释,动态内嵌脚本,和放入外部文件的 CSS 等在页面结构中很小的改变都能够 ...
- js 九九乘法表
/** * 第一行:1x1=1 * 第二行:2x1=2 2x2=4 * 第三行:3x1=3 3x2=6 3x3=9 */ document.write('<table border=" ...
- 今天我看了一个H5游戏EUI的例子,我都快分不清我到底是在用什么语言编译了代码了,作为刚刚学习H5游戏开发的菜鸟只能默默的收集知识
今天看了一个EUI的demo,也是接触H5游戏开发的第五天了,我想看看我能不能做点什么出来,哎,自己写果然还是有问题的.在看EUI哪一个demo的时候就遇见了一些摇摆不定的问题,我觉得提出来 1.to ...
- Angularjs 服务注册
$injector: (When you request a service, the $injector is responsible for finding the correct service ...
- const成员变量初始化总结
const可以用来声明常量也就是说他的值不能被修改: const成员必须在定义的时候同时初始化,不能进行赋值 如 const int a:a的值不能修改,不能给它赋值,如何才能让它一开始就拥有一个值? ...
- [NOIP2015] 斗地主(搜索)
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...