admin管理员组

文章数量:1391925

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?

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?

Share Improve this question edited Jan 10, 2017 at 14:20 usr30911 2,7817 gold badges32 silver badges58 bronze badges asked Jan 9, 2017 at 17:54 Thingdou1993Thingdou1993 511 silver badge2 bronze badges 1
  • correct me if im wrong but your trying to open the camera from a html page correct? – usr30911 Commented Jan 10, 2017 at 13:26
Add a ment  | 

1 Answer 1

Reset to default 4

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./drive/folders/0BwRMp8dK9LMLeEo5cTlXVE9ZUW8?resourcekey=0-6dEjytPymBZvebmmyy9ymQ&usp=sharing

You can also refer these tutorials

  • Upload Image/File from Gallery or Camera in WebView in Android

  • Android Smart WebView with advanced features

  • Open File Chooser with camera option in webview

cheers!.

本文标签: javascriptOpening camera from android webViewStack Overflow