Saturday, March 4, 2017

Choose camera in file upload in cordova application on android without using cordova camera

Leave a Comment

So i made a cordova app, i added android platform and made a simple html with an imput field

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

I have added

<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" android:required="false" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <feature name="http://api.phonegap.com/1.0/camera" /> 

to the manifest file.

but when i press the button I cannot choose to take a new picture with the camera. are there any permission i miss, or anything else ??

I cannot use the cordova take picture functions, it has to be done in pure html.

3 Answers

Answers 1

An old question but I just faced this problem. It turns out that the simplest way to achieve what you want is include these cordova plugins in you app, even if you don't need to use them

cordova-plugin-camera cordova-plugin-media-capture cordova-plugin-device cordova-plugin-file cordova-plugin-media 

With these loaded I found this behaviour

Clicking on

<input type="file" /> 

Ask you to choose from camera, camcorder, microphone or documents

Clicking on

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

Ask you to choose from camera or documents

Clicking on

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

Immediatly start the camera.

Answers 2

After a bit of Googling, this is what I could conclude:

Media capture in mobile browsers still seems to have some issues. Check out this link. The excerpt says:

Actually, it seems that current implementations don’t rely on the capture attribute at all, but only on the type and accept attributes: the browser displays a dialog box in which the user can choose where the file has to be taken, and the capture attribute is not taken into consideration. For example, iOS Safari relies on the accept attribute (not capture) for images and videos (not audio). Even if you don’t use the accept attribute, the browser will let you choose between “Take Photo or Video” and “Choose Existing”

So looks like capture attribute does not make any impact.

Also, suggest you look at this SO post for more info on making this work. Hope it helps. Cheers.

UPDATE: After downvoting, I dig further deep regarding the issue. Most of the searches resulted in no success as the optimum solution for this issue is to use Cordova camera plugin. Finally stumbled on this SO post which is exactly duplicate of this question. The user was able to resolve the issue (using crosswalk web view, though). The answer in that post is mentioned here already by @Fabio. But instead of adding the plugin just for including the permissions, you can make use of cordova-custom-plugin to add the required permissions.

Also as per @jcesarmobile's comment (who is a Cordova expert) in the post without crosswalk web view plugin, input type works fine only on iOS and not on Android. So using camera plugin is the only way to make it work without using crosswalk plugin.Hopefully, it helps.

UPDATE 2: After the updated question, I dug bit more deep for the resolution of this issue. But now I can assure that this issue is still not resolved for Android Webview. Looks like it is not a Cordova issue but issue with Chromium web view.

For detailed info, request you to please look at these issues in Apache Cordova Issue Tracker:

Both these issues are unresolved till date. So I m sure that for now, you cannot make it work on Android unless you use Cordova camera plugin. Hope you agree with me and accept the resolution. Cheers

Answers 3

Here one cool guy uses some new HTML tags to access the camera without phonegap functions and he has a code sample to share.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment