8

my android phone is connected to a web server through a webview. in my HTML I have a button to upload a photo. the user should have the choice to upload an image or take a photo from camera. my HTML code is:

 <div class="takePhoto"> <form id="take" action="" method="POST"> <input type="file" id= "cap" name="personalPhoto" accept="image/*" capture ="camera" id="camera"><p> </form> </div> 

However when I click on the button the file chooser opens and I have the ability to choose images but not to use the camera. Any solution for this?

P.S. in my code the word (capture) doesn't have a special style or color which I find weird and this might be the problem!

2
  • yes and by clicking this button the file browser opens successfully .... but nothing related to the camera Commented May 30, 2017 at 14:31
  • Use <input type="file" id="myinput" accept="image/*;capture=camera" capture> Commented Feb 26, 2022 at 12:53

5 Answers 5

11

In iPhone iOS6 and from Android ICS onwards, HTML5 has the following tag which allows you to take pictures from your device:

 <input type="file" accept="image/*" capture="camera"> 

you can also try

 <input type="file" accept="image/*" capture="capture"> 

or

<input type="file" accept="image/*;capture=camera"> 
Sign up to request clarification or add additional context in comments.

2 Comments

the first suggestion is inside my code and not working! The other two options are not working too
in Adnroid, this solution gives me options for camera along with files as well. I just want to open straight camera in android. how do I do this?
1
<input type="button" value="Say hello" onClick="showCamera('Android! Start Camera')" /> <script type="text/javascript"> function showCamera(toast) { Android.showCamera(toast); } </script> 

Intialise Webview in Java file

WebView webView = (WebView) findViewById(R.id.webview); webView.addJavascriptInterface(new WebAppInterface(this), "android") 

WebInterface :

public class WebAppInterface { Context mContext; static final int REQUEST_IMAGE_CAPTURE = 1; /** Instantiate the interface and set the context */ WebAppInterface(Context c) { mContext = c; } /** Show a toast from the web page and here tell to open camera */ @JavascriptInterface public void showCamera(String toast) { Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); dispatchTakePictureIntent(); } /** Handle Camera result*/ private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { mContext.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); } } } 

1 Comment

Thank you for your suggestion. Actually I tried it, when I press the button nothing happens except for the message 'Android! Start Camera' it is displayed on screen. That means the function is called but not everything works correctly! and in the line: mContext.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); Android studio says: cannot resolve method startActivityForResult. Till this point I can't get it to work.
1

I have this kind of problem, nowadays. And I think that I find out the solution. But you have to do something more with my solution. You can check this. https://simpl.info/imagecapture/ After checking this, please share with me your experience. Thanks

Comments

1

i have tested this method and it worked for mobile devices

<input type="file" capture="user" accept="image/"/> 

Comments

0

You can interface android native code with javascript in order to capture the click on a button and have android code to send an intent to open the cammera, take the photo, and store it in some path.

Then, in onActivityResult you will take care of getting the photo from the path and uploading it to the webserver. To do this one way is to encode the bitmap in a base64 string and send it in a POST form using an android http client.

I will show how to do the interface between javscript and android (which is what the quesiton is about), all the rest regarding manipulating the foto has its own complexities and there are many tutorials about it.

Main Activity

public class MainActivity extends AppCompatActivity { private WebView webView; private Bitmap imageBitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = (WebView) findViewById(R.id.webview); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); webView.addJavascriptInterface(new WebAppInterface(this),"Android"); webView.loadData( "<Button id=\"btnTakePhoto\" onClick=\"takePhoto()\">TakePhoto</Button><div class=\"takePhoto\">\n" + "<script type=\"text/javascript\">" + "function takePhoto() {\n" + " Android.takePhoto();\n" + " }\n" + "</script>" + "</div>", "text/html","UTF-8"); } public class WebAppInterface { Context mContext; /** Instantiate the interface and set the context */ WebAppInterface(Context c) { mContext = c; } /** The android function calld from javascript */ @JavascriptInterface public void takePhoto() { dispatchTakePictureIntent(); } } /* This gests a Thumbnail only*/ static final int REQUEST_IMAGE_CAPTURE = 1; private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { Bundle extras = data.getExtras(); imageBitmap = (Bitmap) extras.get("data"); /* Encode bitmap as base64 and transmit to server */ } } } 

NOTE: That this kind of interfacing is insecure on Andriod below 17.

EDIT 1

The html

 <html> <body> <div class="takePhoto"> <Button onClick="takePhoto()">Take Photo </Button> </div> <script type="text/javascript"> function takePhoto(){ Android.takePhoto(); } </script> </body> </html> 

EDIT 2

In the manifest I only added:

<uses-feature android:name="android.hardware.camera" android:required="true" /> 

For your app you also need INTERNET permission.

7 Comments

I have integrated this code in my android studio MainActivity ... but how do I call it from the webpage? can you give me a simple HTML/Javascript code to call this function?
It is in the example code where it says webview.loadData, you have a button with the onCllick pointing to the javascript function. And under that the javascript function calling the android function. You can put the html/javascript on a webpage and call loadUrl instead.
I'm sorry for asking many questions but I'm very new to android and I really appreciate your help ... Actually in the onCreate function I'm loading my web page from the server using: webView.loadUrl("192.168.43.115/myPage.php"); in this page I have the button that should call the camera so how to connect it with your code?
to the button add the onClick="takePhoto()" and add the script tag before </body>. But notice that this code will work only on Android.
I did this: <html> <body> <div class="takePhoto"> <form id="take" method="POST"> <input class="buttom" name="Take a Photo" id="submit" onClick="takePhoto()" tabindex="4" value="Take" type="submit"> </form> </div> <script type=\"text/javascript\"> function takePhoto() Android.takePhoto(); </script> </body> </html> Nothing Happened ...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.