标签:
Webview下面坑奇多,直接在浏览器下面可以直接调出相机和文件管理,但是在webview下面就是不行,在网上查阅了很多文章,不得不说坑奇多,最多终于拼拼凑凑成功了,
安卓版本为4.4.2,调试通过.
安卓端关键源码:
protected ValueCallback<Uri> mUploadMessage;
protected int FILECHOOSER_RESULTCODE = 1;
private String mCameraFilePath;
@SuppressLint("SdCardPath")
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (null == mUploadMessage)
return;
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
if (result == null && intent == null && resultCode == Activity.RESULT_OK) {
File cameraFile = new File(mCameraFilePath);
if (cameraFile.exists()) {
result = Uri.fromFile(cameraFile);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));
}
}
}
this.wv.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view,int progress)
{
//载入进度改变而触发
if(progress==100){
handler.sendEmptyMessage(1);//如果全部载入,隐藏进度对话框
}
super.onProgressChanged(view, progress);
}
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
if (mUploadMessage != null)
return;
mUploadMessage = uploadMsg;
startActivityForResult(createDefaultOpenableIntent(), FILECHOOSER_RESULTCODE);
}
// For Android < 3.0
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
openFileChooser(uploadMsg, "");
}
// For Android > 4.1.1
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
openFileChooser(uploadMsg, "");
}
private Intent createDefaultOpenableIntent() {
// Create and return a chooser with the default OPENABLE
// actions including the camera, camcorder and sound
// recorder where available.
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
Intent chooser = createChooserIntent(createCameraIntent());
chooser.putExtra(Intent.EXTRA_INTENT, i);
return chooser;
}
private Intent createChooserIntent(Intent... intents) {
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents);
chooser.putExtra(Intent.EXTRA_TITLE, "请选择");
return chooser;
}
private Intent createCameraIntent() {
// 注意:此处代码主要目的是将拍照文件保存在 browser-photos 文件夹下(非系统默认文件夹)
// 如不需要这样处理,可以简化代码
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File externalDataDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File cameraDataDir = new File(externalDataDir.getPath() + "/Images/");
cameraDataDir.mkdirs();
mCameraFilePath = cameraDataDir.getPath()+ System.currentTimeMillis() + ".jpg";
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCameraFilePath)));
return cameraIntent;
}
});
网页端上传:
var fd = new FormData();
$("#UpPrompt").html("<span style=‘font-size:12px;‘>上传中... </span>");
fd.append("UpFile", $("#UpFile").get(0).files[0]);
$.ajax({
url: "upload.aspx",
type: "POST",
processData: false,
contentType: false,
data: fd,
success: function (addr) {
if (addr != "") {
var ImgText = $("#DisplayImgList").html();
ImgText = ImgText + "<img onclick=‘LoadImg(this);‘ class=‘CommentImg‘ style=‘width:40px;height:40px;-webkit-border-radius: 8px;margin-right:3px;‘ src=‘" + addr + "‘>"
$("#UpPrompt").html("");
$("#DisplayImgList").html(ImgText)
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$("#UpPrompt").html("上传失败");
//alert(XMLHttpRequest.responseText);
}
});
//解决upfile控件change事件只能响应一次
var jqObj = $("#UpFile");
jqObj.val("");
var domObj = jqObj[0];
domObj.outerHTML = domObj.outerHTML;
var newJqObj = jqObj.clone();
jqObj.before(newJqObj);
jqObj.remove();
$("#UpFile").unbind().change(function (e) {
UpLoadFile();
});
网页端服务器源码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Web.CustPhone
{
public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Files[0].FileName != "")
{
string FILEDIR = "/UpLoadImg";
string fileName = "";
string destFileName = "";
string nFileDir = "";
string strAdjunctFile = "";
string fileTailName =
fileTailName = System.IO.Path.GetExtension(Request.Files[0].FileName);
//***构造上载文件的名称
String fileTime = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString()
+ DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString()
+ DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
+ DateTime.Now.Millisecond.ToString();
fileName = fileTime + ".jpg";
nFileDir = FILEDIR + "/" + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString().PadLeft(2, ‘0‘);
if (Directory.Exists(Server.MapPath(Request.ApplicationPath) + nFileDir) == false)
{
Directory.CreateDirectory(Server.MapPath(Request.ApplicationPath) + nFileDir);
}
strAdjunctFile = nFileDir + fileName;
destFileName = Server.MapPath(Request.ApplicationPath) + strAdjunctFile;
destFileName = Server.UrlDecode(destFileName);
Request.Files[0].SaveAs(destFileName);
Response.Write(Request.ApplicationPath + strAdjunctFile.ToString());
}
else
{
Response.Write("");
}
}
}
}
再次感谢各位网上的文章,因为参照的太多,本文android内容均是转发.
Android WebView环境下打开相机、文件管理进行上传
标签:
原文地址:http://www.cnblogs.com/littlewrong/p/4793614.html