Can I open camera in webview?

2019-01-14 18:21发布

问题:

Can I open android camera in webview?

回答1:

The easiest way to have the camera functionality when using a Webview would be the use an Intent.

If you use the API you have to build a lot of the UI yourself. This is good or bad depending on what you need to do in your application and how much control you need over the "picture taking process". If you just need a quick way to snap a photo and use it in your application, Intent is the way to go.

Intent Example:

private Uri picUri;
private void picture()
{
     Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
     File photo = new File(Environment.getExternalStorageDirectory(), "pic.jpg");
     cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
     picUri = Uri.fromFile(photo);
     startActivityForResult(cameraIntent, TAKE_PICTURE);
}

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode){
        case TAKE_PICTURE:
            if(resultCode == Activity.RESULT_OK){
                Uri mypic = picUri;
                //Do something with the image.
            }
}

I borrowed parts of this example from another answer to build this originally. But I don't have the URL anymore.

In the app I am writing now, I convert this image to Base64 and then pass it to Javascript which then posts it to my server. But, that's probably more than you needed to know. :)

here is the link to make it work on webView



回答2:

As far as I know this isn't built into the Android API directly. However, you can use PhoneGap, which provides Javascript hooks into native device functionality (i.e., the camera).

You can view a list of supported features here, and read their Camera API documentation here.