[英]Opening camera from android webView
I'm trying to open the android native camera from an html page loaded in a android webView by using HTML input type file tag. 我试图通过使用HTML输入类型文件标签从Android webView中加载的html页面打开android本机相机。
<input type="file" accept="image/*">
I have no idea why but the camera is not opening and I don't know what to do. 我不知道为什么但相机没有打开,我不知道该怎么办。
I've tried the same page on a iPhone webView and it's working. 我在iPhone webView上尝试了相同的页面,它正在运行。
What can I do? 我能做什么?
If i understand your question correctly 如果我正确理解你的问题
You want to open the android device camera on click of a button in the webpage(html)? 你想点击网页上的按钮(html)打开Android设备相机?
On the basis of that assumption, You need to do the following 在此假设的基础上,您需要执行以下操作
Use a JavascriptInterface 使用JavascriptInterface
public class WebVCamBridgeInterface {
/**
* Javacript function to start native camera
*/
@JavascriptInterface
public void takePicture() {
captureImage();
}
/**
* Javascript function to start the GalleryActivity for user to choose the image to be uploaded
*/
@JavascriptInterface
public void showPictures() {
Intent intent = new Intent(LandingActivity.this, GalleryActivity.class);
startActivityForResult(intent, Constants.REQ_GALLERY);
}
}
add JSinterface to your webview 将JSinterface添加到您的webview中
webView.addJavascriptInterface(new WebVCamBridgeInterface (), "AndroidDevice");
Have the following JS in your html/web page 在您的html /网页中有以下JS
<script>
function takePicture() {
if(typeof AndroidDevice !== "undefined"){
AndroidDevice.takePicture();
}
}
function showPictures() {
if(typeof AndroidDevice !== "undefined"){
AndroidDevice.showPictures();
}
}
function imageData(data){
document.getElementById('displayImage').setAttribute( 'src', 'data:image/png;base64,'+data );
if(typeof AndroidDevice !== "undefined"){
}
}
</script>
Im providing the link to a sample project with video of a demo ,have a look. 我提供了一个带有演示视频的示例项目的链接,看看。 https://drive.google.com/drive/folders/0BwRMp8dK9LMLeEo5cTlXVE9ZUW8?usp=sharing
https://drive.google.com/drive/folders/0BwRMp8dK9LMLeEo5cTlXVE9ZUW8?usp=sharing
You can also refer these tutorials 您也可以参考这些教程
Upload Image/File from Gallery or Camera in WebView in Android 在Android中的WebView中从图库或相机上传图像/文件
Android Smart WebView with advanced features 具有高级功能的Android Smart WebView
Open File Chooser with camera option in webview 在webview中使用相机选项打开文件选择器
cheers!. 干杯!。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.