27

In a PhoneGap app I tried to use camera using HTML5 input tag like this.

  1. create new project using CLI
  2. Add iOS and Android platform
  3. Add camera plugin
  4. build for both devices
  5. run on both devices (Actual device IPhone 5 with iOS 7.1.2 and Android 4.4.2 (Samsung Note))

Following is the code line that I tried to execute

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

It works in iPhone but not in Android. What have I missed? Or is this not supported in Android? After tapping on the field in Android nothing happens while in iPhone it acts like below File input working in iOS

jaypeagi
  • 3,133
  • 18
  • 33
Blu
  • 4,036
  • 6
  • 38
  • 65
  • imo its not supported properly in android, check [cordova jira](https://issues.apache.org/jira/browse/CB-5294) and [this thread](http://stackoverflow.com/questions/19882331/html-file-input-in-android-webview-android-4-4-kitkat) – turtle Nov 27 '14 at 08:47
  • @turtle mmm. so this is worst news for me :(. Anyways thanks for response – Blu Nov 27 '14 at 09:12
  • If you just want photos, use the camera plugin you already included. – jcesarmobile Nov 27 '14 at 09:33
  • If i use camera plugin. Should i have to mange Take Photo and Choose Existing optional manually or they will popup like they work in type="file" case? @jcesarmobile – Blu Nov 27 '14 at 10:46
  • 1
    you have to do it manually, you can use the dialogs plugin confirm, to make the user choose between camera of photolibrary and then change the Camera.sourceType to Camera.PictureSourceType.CAMERA or Camera.PictureSourceType.SAVEDPHOTOALBUM – jcesarmobile Nov 27 '14 at 10:57
  • Thanks for all help. Now i am start with that points. – Blu Nov 27 '14 at 11:01
  • 3
    We had the same problem while working on one of our projects. After exploring a lot we found some solutions and able to run it on android. But, there is still some issues with the android version 4.4-4.4.2 because these versions do not support it. For more details of the problem and its solution, visit the below link: http://www.ipragmatech.com/fix-html5-file-input-cordova-android/ – iPragmatech Mar 23 '17 at 05:07
  • Have you added the file permission in the manifest? Without file access permission, the button won't do nothing – Warface Sep 30 '20 at 23:09
  • Have you checked the styling of the elements surrounding the control? It could be one element is convering on android and not on iOS. This is the most common reason for this that I have found, but without code, I don't want to make that an answer. – Tyler Lazenby Apr 21 '21 at 16:23

1 Answers1

1

I see this is an old question, but the answer is not using HTML but JavaScript and a cordova plugin.

I used this plugin

Quite easy to use.

Example:

if (device.platform == "Android") { //Here we check to see what OS is used. input with type file is supported on iOS, but not Android. So use plugin for Android
    fileStorage.open(function (uri) {
        if (callback) {
            callback(uri);
        }
        return uri;
    }, function (ex) {
        throw ex;
    });
} else {
    var file = document.createElement("input");
    file.type = "file";
    file.accept = ".pdf";
    file.style.display = "none";

    document.body.appendChild(file);
    file.click();

    file.onchange = function (f) {
        console.log(f);

        console.log(file.files);

        var reader = new FileReader()

        reader.readAsDataURL(file.files[0]);

        reader.onloadend = function () {
            if (callback) {
                callback(reader.result);
            }

            return reader.result;
        }

        document.body.removeChild(file);
    }
}
Blu
  • 4,036
  • 6
  • 38
  • 65
Marius
  • 1,420
  • 1
  • 11
  • 19