[转]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 ...
随机推荐
- .NET涉及的一些名词
本文在最为概略的层次上对.NET涉及的一些名词进行解释, 包括: 通用语言基础架构(Common Language Infrastructure, CLI). 虚拟执行系统(Virtual Execu ...
- 微软雅黑 在css里怎么写
1.首先要了解css中是如何控制字体的. font:在一个声明中设置所有字体属性: font有以下几个属性: font-style:字体样式 font-variant:字体异体 font-weight ...
- bn
BN是在每一层之前对神经元的输入进行归一化,对sigmoid激活函数有效(对Relu也有效),可以更快的收敛且可以有效减少过拟合.
- help man info 三个的区别
“--help”选项 “--help”是一个工具选项,大部分的GNU工具都具备这个选项,“--help”选项可以用来显示一些工具的信息 “man”工具 Man工具可以显示系统手册页中的内容,这些内容大 ...
- Spark RDD API详解(一) Map和Reduce
RDD是什么? RDD是Spark中的抽象数据结构类型,任何数据在Spark中都被表示为RDD.从编程的角度来看,RDD可以简单看成是一个数组.和普通数组的区别是,RDD中的数据是分区存储的,这样不同 ...
- lnmp搭建的常见错误
1:运行nginx时的错误 ./configure: error: the HTTP rewrite module requires the PCRE library. 解决: [root@svr11 ...
- {part1}DFN+LOW(tarjan)割点
什么是jarjan? 1)求割点 定义:在无向连通图中,如果去掉一个点/边,剩下的点之间不连通,那么这个点/边就被称为割点/边(或割顶/桥). 意义:由于割点和割边涉及到图的连通性,所以快速地求出割点 ...
- 11.12模拟考T1(可持续优化)PS:神奇的东西
1.数列操作 (array.pas/c/cpp) [问题描述] 现在有一个数列,最初包含0个数.现在要对数列操作n次,操作有3类. 1) a k,在数列的最后插入一个整数k 2) s 将最近插入的 ...
- UVA 1395 (kruskal)
/* 最大路与最小路的问题: 这道题看似简单,但是若不知道思路将无法写出. 思路:最小生成树很容易求出,但是最大值与最小值只差很难保证是最小的, 比如:1 5 5 6 100 101 很明显101 - ...
- jsp中查询条件的回显
后台框架为ssh,前台纯手写无框架是最老的写法,因为是接手别人的项目无法改变框架原型,只能基于修改. 进入正题: 我这里查询条件有两种input的text(文本框)和select(下拉框). 1.te ...