Can I open android camera in webview?
-
possible duplicate of [Calling camera from an activity, capturing an image and uploading to a server](http://stackoverflow.com/questions/10679571/calling-camera-from-an-activity-capturing-an-image-and-uploading-to-a-server) – Kate Gregory Oct 18 '12 at 19:49
2 Answers
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

- 13
- 5

- 5,495
- 4
- 38
- 53
-
-
1@David Souther I am not sure if you mean from the Webview on the Android side, or the browser side. On the Android side, you would call it like any other Java method. If you want to call it from Javascript, like, have the user click an tag and the camera opens, then you can write a Java class that acts as the interface. Add the class like this: _webview_.addJavascriptInterface(new JavaScriptInterface(this), "Android"); In JS, you would call it like this: Android._method_(); – Jonathan Apr 29 '11 at 19:31
-
@David Souther Sorry for the underscores. Wanted italics, didn't figure it out until the edit time was up. I am new. :S BTW, this link explains the JS interface better: http://developer.android.com/guide/webapps/webview.html#BindingJavaScript – Jonathan Apr 29 '11 at 19:38
-
@piyush Not too sure about phonegap. A quick Google Search lead me here: http://docs.phonegap.com/phonegap_camera_camera.md.html EDIT: Looks like Donut had a link down there already. – Jonathan May 03 '11 at 12:10
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.

- 110,061
- 20
- 134
- 146