写着文章的初衷还是想让后来者少走一点弯路,度娘出来的确实坑爹,教程基本都是老的,只能调用文件或只能调用摄像头,本次测试再android 8.0上小米pad上测试过,需要的拿走
先上个图,形象一些
![](https://img.haomeiwen.com/i2520656/8073dc3ed0b2c4aa.jpg)
权限这块是
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- 在sdcard中创建/删除文件的权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
本地测试html 放在assets目录下
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional// EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<title>H5调用手机相册摄像头以及文件夹</title>
<style>
input{outline: none;height: 30px;line-height: 30px;display:inline-block;font-size: 1rem;width: 50%;border:none;padding-left: 20px;color: #a8a8a8;}
#takepicture_1{width: 15%;height: 32px; margin-right: 8%;border:none; background: url(images/camera_03.png) no-repeat;display: inline-block;float: right;outline: none;padding-left: 0px;}
#takepicture{width:100% !important;height:32px!important;border:none !important; background: url(images/camera_03.png) no-repeat !important;display: inline-block !important;float: right !important;outline: none !important;padding-left: 0px !important;opacity: 0;}
.images{width: 100%;height: auto;text-align: center;}
#preview{max-width: 100%;height: auto;display: inline-block;margin:auto;}
</style>
</head>
<body>
<p>上 传:<input name="kahao" type="number" id="cardnum" size="15" placeholder="点击输入或者上传图片"
onfocus="if (placeholder =='点击输入或者上传图片'){placeholder =''}"
onblur="if (placeholder ==''){placeholder='点击输入或者上传图片'}"/>
<span id="takepicture_1">
<input class="btn" id="takepicture" type="file"
onchange="javascript:setImagePreview();"/></span>
</p>
<div class="images" id="localImag">
<img id="preview" width="-1" height="-1" style="display: none"/>
</div>
<script type="text/javascript">
function setImagePreview() {
var preview, img_txt, localImag, takepicture = document.getElementById("takepicture"),
picture = takepicture.value;
if (!picture.match(/.jpg|.gif|.png|.bmp/i)) return alert("您上传的图片格式不正确,请重新选择!"),
!1;
if (preview = document.getElementById("preview"), takepicture.files && takepicture.files[0]) preview.style.display = "block",
preview.style.width = "200px",
preview.style.height = "150px",
preview.src = window.navigator.userAgent.indexOf("Chrome") >= 1 || window.navigator.userAgent.indexOf("Safari") >= 1 ? window.URL.createObjectURL(takepicture.files[0]) : window.URL.createObjectURL(takepicture.files[0]);
else {
takepicture.select(),
takepicture.blur(),
img_txt = document.selection.createRange().text,
localImag = document.getElementById("localImag"),
localImag.style.width = "200px",
localImag.style.height = "150px";
try {
localImag.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)",
localImag.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = img_txt
} catch(f) {
return alert("您上传的图片格式不正确,请重新选择!"),
!1
}
preview.style.display = "none",
document.selection.empty()
}
}
</script>
</body>
</html>
核心代码是重写 onShowFileChooser
//Handling input[type="file"] requests for android API 21+
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if (check_permission(2) && check_permission(3)) {
if (ASWP_FUPLOAD) {
if (asw_file_path != null) {
asw_file_path.onReceiveValue(null);
}
asw_file_path = filePathCallback;
Intent takePictureIntent = null;
if (ASWP_CAMUPLOAD) {
takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(TestActivity.this.getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = create_image();
takePictureIntent.putExtra("PhotoPath", asw_cam_message);
} catch (IOException ex) {
Log.e("TAG", "Image file creation failed", ex);
}
if (photoFile != null) {
asw_cam_message = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
} else {
takePictureIntent = null;
}
}
}
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
if (!ASWP_ONLYCAM) {
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType(ASWV_F_TYPE);
if (ASWP_MULFILE) {
contentSelectionIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
}
}
Intent[] intentArray;
if (takePictureIntent != null) {
intentArray = new Intent[]{takePictureIntent};
} else {
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, getString(R.string.fl_chooser));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, asw_file_req);
}
return true;
} else {
get_file();
return false;
}
}
回调结果也需要重写
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (Build.VERSION.SDK_INT >= 21) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimary));
Uri[] results = null;
if (resultCode == Activity.RESULT_OK) {
if (requestCode == asw_file_req) {
if (null == asw_file_path) {
return;
}
if (intent == null || intent.getData() == null) {
if (asw_cam_message != null) {
results = new Uri[]{Uri.parse(asw_cam_message)};
}
} else {
String dataString = intent.getDataString();
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
} else {
if (ASWP_MULFILE) {
if (intent.getClipData() != null) {
final int numSelectedFiles = intent.getClipData().getItemCount();
results = new Uri[numSelectedFiles];
for (int i = 0; i < numSelectedFiles; i++) {
results[i] = intent.getClipData().getItemAt(i).getUri();
}
}
}
}
}
}
}
asw_file_path.onReceiveValue(results);
asw_file_path = null;
} else {
if (requestCode == asw_file_req) {
if (null == asw_file_message) return;
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
asw_file_message.onReceiveValue(result);
asw_file_message = null;
}
}
}
估计你看累了
拿着源码去happy
网友评论