Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
352 views
Welcome To Ask or Share your Answers For Others

1 Answer

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?resourcekey=0-6dEjytPymBZvebmmyy9ymQ&usp=sharing

You can also refer these tutorials

cheers!.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...