从android webView打开相机 [英] Opening camera from android webView

查看:126
本文介绍了从android webView打开相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



<$ p $

我试图从加载在android webView
中的html页面打开android本机相机。 p> < input type =fileaccept =image / *>

我不知道为什么,但相机没有打开,我不知道该怎么做。



我试过iPhone webView上的同一页面,它正在工作。

我该怎么办?

解决方案

如果我正确理解您的问题,您可以打开点击一个按钮在网页(HTML)的Android设备相机?

在这个假设的基础上,
您需要执行以下操作使用 JavascriptInterface

  public class WebVCamBridgeInterface {
/ **
* Javacript函数启动本机相机
* /
@JavascriptInterface
public void takePicture(){
captureImage();

$ b $ ** b $ b * Javascript函数启动GalleryActivity供用户选择要上传的图片
* /
@JavascriptInterface
public void showPictures(){
Intent intent = new Intent(LandingActivity.this,GalleryActivity.class);
startActivityForResult(intent,Constants.REQ_GALLERY);





$ p $添加JS接口到你的webview p>

  webView.addJavascriptInterface(new WebVCamBridgeInterface(),AndroidDevice); 

在您的html / web页面中有以下JS

 < script> 
函数takePicture(){
if(typeof AndroidDevice!==undefined){
AndroidDevice.takePicture();



函数showPictures(){
if(typeof AndroidDevice!==undefined){
AndroidDevice.showPictures();



函数imageData(data){
document.getElementById('displayImage')。setAttribute('src','data:image / png; base64,'+ data);
if(typeof AndroidDevice!==undefined){
}
}
< / script>

我提供了一个带示例视频的示例项目链接,请看看。
https://drive.google.com/drive/folders/ 0BwRMp8dK9LMLeEo5cTlXVE9ZUW8?usp =分享


您也可以参考这些教程



干杯!


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.

<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.

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)?

On the basis of that assumption, You need to do the following

Use a 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

webView.addJavascriptInterface(new WebVCamBridgeInterface (), "AndroidDevice");

Have the following JS in your html/web page

<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

You can also refer these tutorials

cheers!.

这篇关于从android webView打开相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆