在Android浏览器中通过WebView调用相机拍照/选择文件 上传到服务器
2015-07-14 11:13
846 查看
最近做的一个项目中,有这样一个要求,在浏览器中调用系统的拍照功能或者选择文件,然后将文件上传到服务器,类似修改头像。 简单而言,就是在一个html页面中有这样一段代码 <input class="filePrew" type="file" capture="camera" accept="image/*" name="image">
刚开始的时候,没有感觉很难的,因为在UC浏览器、系统自带的浏览器中都可以进行拍照/文件管理器选择,可是在自己所写的Activity中却不行。后来实在是没有思路了,就在网上找了一下,发现要 实现这种功能,都是在webview的WebChromeClient中覆盖掉openFileChooser方法,注意openFileChooser方法在WebChromeClient中有@hide标记。这里只管重写即可,下面将主要代码贴出来,做个记录
[java] view
plaincopy
private ValueCallback<Uri> mUploadFile;
/**拍照/选择文件请求码*/
private static final int REQUEST_UPLOAD_FILE_CODE = 12343;
private void setWebChromeClient()
{
if (null != mMainWebView)
{
mMainWebView.setWebChromeClient(new WebChromeClient()
{
// Andorid 4.1+
public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType, String capture)
{
openFileChooser(uploadFile);
}
// Andorid 3.0 +
public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType)
{
openFileChooser(uploadFile);
}
// Android 3.0
public void openFileChooser(ValueCallback<Uri> uploadFile)
{
// Toast.makeText(WebviewActivity.this, "上传文件/图片",Toast.LENGTH_SHORT).show();
mUploadFile = uploadFile;
startActivityForResult(Intent.createChooser(createCameraIntent(), "Image Browser"), REQUEST_UPLOAD_FILE_CODE);
}
});
}
}
private Intent createCameraIntent()
{
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//拍照
//=======================================================
Intent imageIntent = new Intent(Intent.ACTION_GET_CONTENT);//选择图片文件
imageIntent.setType("image/*");
//=======================================================
return cameraIntent;
}
//最后在OnActivityResult中接受返回的结果
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_UPLOAD_FILE_CODE && resultCode == RESULT_OK)
{
if (null == mUploadFile)
{
return;
}
Uri result = (null == data) ? null : data.getData();
if (null != result)
{
ContentResolver resolver = this.getContentResolver();
String[] columns = { MediaStore.Images.Media.DATA };
Cursor cursor = resolver.query(result, columns, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(columns[0]);
String imgPath = cursor.getString(columnIndex);
System.out.println("imgPath = " + imgPath);
if (null == imgPath)
{
return;
}
File file = new File(imgPath);
//将图片处理成大小符合要求的文件
result = Uri.fromFile(handleFile(file));
mUploadFile.onReceiveValue(result);
mUploadFile = null;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/**处理拍照/选择的文件*/
private File handleFile(File file)
{
DisplayMetrics dMetrics = getResources().getDisplayMetrics();
BitmapFactory.Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
int imageWidth = options.outWidth;
int imageHeight = options.outHeight;
System.out.println(" imageWidth = " + imageWidth + " imageHeight = " + imageHeight);
int widthSample = (int) (imageWidth / (dMetrics.density * 90));
int heightSample = (int) (imageHeight / (dMetrics.density * 90));
System.out.println("widthSample = " + widthSample + " heightSample = " + heightSample);
options.inSampleSize = widthSample < heightSample ? heightSample : widthSample;
options.inJustDecodeBounds = false;
Bitmap newBitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
System.out.println("newBitmap.size = " + newBitmap.getRowBytes() * newBitmap.getHeight());
File handleFile = new File(file.getParentFile(), "upload.png");
try
{
if (newBitmap.compress(CompressFormat.PNG, 50, new FileOutputStream(handleFile)))
{
System.out.println("保存图片成功");
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return handleFile;
}
这样就可以在WebView中上传文件了。记得要添加相应的权限!
参考:http://developer.android.com/about/versions/android-3.0.html
http://blog.sina.com.cn/s/blog_5749ead90101clrn.html
刚开始的时候,没有感觉很难的,因为在UC浏览器、系统自带的浏览器中都可以进行拍照/文件管理器选择,可是在自己所写的Activity中却不行。后来实在是没有思路了,就在网上找了一下,发现要 实现这种功能,都是在webview的WebChromeClient中覆盖掉openFileChooser方法,注意openFileChooser方法在WebChromeClient中有@hide标记。这里只管重写即可,下面将主要代码贴出来,做个记录
[java] view
plaincopy
private ValueCallback<Uri> mUploadFile;
/**拍照/选择文件请求码*/
private static final int REQUEST_UPLOAD_FILE_CODE = 12343;
private void setWebChromeClient()
{
if (null != mMainWebView)
{
mMainWebView.setWebChromeClient(new WebChromeClient()
{
// Andorid 4.1+
public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType, String capture)
{
openFileChooser(uploadFile);
}
// Andorid 3.0 +
public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType)
{
openFileChooser(uploadFile);
}
// Android 3.0
public void openFileChooser(ValueCallback<Uri> uploadFile)
{
// Toast.makeText(WebviewActivity.this, "上传文件/图片",Toast.LENGTH_SHORT).show();
mUploadFile = uploadFile;
startActivityForResult(Intent.createChooser(createCameraIntent(), "Image Browser"), REQUEST_UPLOAD_FILE_CODE);
}
});
}
}
private Intent createCameraIntent()
{
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//拍照
//=======================================================
Intent imageIntent = new Intent(Intent.ACTION_GET_CONTENT);//选择图片文件
imageIntent.setType("image/*");
//=======================================================
return cameraIntent;
}
//最后在OnActivityResult中接受返回的结果
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_UPLOAD_FILE_CODE && resultCode == RESULT_OK)
{
if (null == mUploadFile)
{
return;
}
Uri result = (null == data) ? null : data.getData();
if (null != result)
{
ContentResolver resolver = this.getContentResolver();
String[] columns = { MediaStore.Images.Media.DATA };
Cursor cursor = resolver.query(result, columns, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(columns[0]);
String imgPath = cursor.getString(columnIndex);
System.out.println("imgPath = " + imgPath);
if (null == imgPath)
{
return;
}
File file = new File(imgPath);
//将图片处理成大小符合要求的文件
result = Uri.fromFile(handleFile(file));
mUploadFile.onReceiveValue(result);
mUploadFile = null;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/**处理拍照/选择的文件*/
private File handleFile(File file)
{
DisplayMetrics dMetrics = getResources().getDisplayMetrics();
BitmapFactory.Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
int imageWidth = options.outWidth;
int imageHeight = options.outHeight;
System.out.println(" imageWidth = " + imageWidth + " imageHeight = " + imageHeight);
int widthSample = (int) (imageWidth / (dMetrics.density * 90));
int heightSample = (int) (imageHeight / (dMetrics.density * 90));
System.out.println("widthSample = " + widthSample + " heightSample = " + heightSample);
options.inSampleSize = widthSample < heightSample ? heightSample : widthSample;
options.inJustDecodeBounds = false;
Bitmap newBitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
System.out.println("newBitmap.size = " + newBitmap.getRowBytes() * newBitmap.getHeight());
File handleFile = new File(file.getParentFile(), "upload.png");
try
{
if (newBitmap.compress(CompressFormat.PNG, 50, new FileOutputStream(handleFile)))
{
System.out.println("保存图片成功");
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return handleFile;
}
这样就可以在WebView中上传文件了。记得要添加相应的权限!
参考:http://developer.android.com/about/versions/android-3.0.html
http://blog.sina.com.cn/s/blog_5749ead90101clrn.html
相关文章推荐
- AndroidManifest.xml文件内容详解
- Kinect2.0 mapping 深度相机图像和颜色相机图像
- Android -- Volley
- 关于GSON中 java.lang.reflect.Type type = new TypeToken<List<Object>>(){}.getType();
- Android studio十大常用快捷键
- IOS风格的时间选择器在安卓中的简单实现
- 通过web服务器访问MYSQL数据库,使其数据同步到android SQLite数据库
- Android自定义RatingBar
- jap中PAGE,request,session和application的区别
- js 处理移动端触摸事件
- Android中不同参数的Timer.schedule()方法之间的区别
- Android 插件开发-热补丁解决线上问题
- android Activity切换动画
- cocos2d-x 3.2 win7环境配置(一)
- Appium 环境搭建
- ObjectiveC 深浅拷贝学习
- android实现下拉框(spinner),自定义大小颜色背景位置,去掉默认样式黑边
- Android 图片(文件--带参数)批量上传到服务器
- Android 在项目中实现百度地图
- Android多线程下安全访问数据库