Cordova Android Camera- giving illegal argument exception-ThrowExceptions

Exception or error:

I started cordova Android programming 2 month back. Everything was fine mainly camera that is not working now. But last week some how my project directory got deleted and I reinstall all plugin of cordova and complied project but now its showing error ‘illegal argument exception’ on camera click surprising thing is i didn’t change single line of code and it’s also working properly on other developer machine.

cordova plugin add cordova-plugin-camera
command used from camera plugin installation.

camera_app.js file

var pictureSource; // picture source
var destinationType; // sets the format of returned value


// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// device APIs are available
//
function onDeviceReady() {
    pictureSource = navigator.camera.PictureSourceType;
    destinationType = navigator.camera.DestinationType;

}


// Called when a photo is successfully retrieved
//
function onPhotoURISuccess(imageURI) {
    // largeImage.src = imageURI;
    console.log(imageURI);
    console.log(document_type);


}

function getPhoto(docType) {
    // Retrieve image file location from specified source
    document_type = docType
    $('.docError').hide();
    navigator.camera.getPicture(onPhotoURISuccess, onFail, {
        quality: 50,
        destinationType: destinationType.FILE_URI
    });
}


$('#file1').click(function() {
    console.log('On camera click');
    getPhoto("addressProof");
});

I tried to debug code finally found it’s due to provider path-
java.lang.IllegalArgumentException: Missing android.support.FILE_PROVIDER_PATHS meta-data

but I have already added provider path meta data in Android xml file, as its automatically gets added when you add cordova plugin.

 <provider android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true" android:name="android.support.v4.content.FileProvider">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
        </provider>

cordova plugin –

cordova-plugin-app-version 0.1.9 “AppVersion”
cordova-plugin-camera 2.4.1 “Camera”
cordova-plugin-compat 1.1.0 “Compat”
cordova-plugin-whitelist 1.3.2 “Whitelist”
cordova-sqlite-storage 1.4.9 “Cordova sqlite storage plugin”
ionic-plugin-keyboard 2.2.1 “Keyboard”

cordova platform-

Installed platforms:
android 6.2.3
Available platforms:
blackberry10 ~3.8.0 (deprecated)
browser ~4.1.0
webos ~3.7.0
windows ~5.0.0

NPM version-
{ npm: ‘3.10.3’,
ares: ‘1.10.1-DEV’,
http_parser: ‘2.7.0’,
icu: ‘57.1’,
modules: ’48’,
node: ‘6.4.0’,
openssl: ‘1.0.2h’,
uv: ‘1.9.1’,
v8: ‘5.0.71.60’,
zlib: ‘1.2.8’ }

How to solve:

It was very simple and basic. Just add your file provider below to your cordova launcher activity. In my case I was having some custom activity and by mistake provider was added at bottom of all activity and my guess is cordova not able to read that provider that’s why it was giving
java.lang.IllegalArgumentException: Missing android.support.FILE_PROVIDER_PATHS meta-data

###

I had a similar problem today. Tried everything (reinstall plugin, reinstall plugin from source, reinstall older version of plugin) but nothing seemed to help.
Don’t know why but after
– killing the emulator
– > cordova clean android
– > cordova platform remove android
– > cordova platform add android
– > cordova build android
– connecting my device
– > cordova run android
it works again as before 🙂
I still don’t know why.
Try it on your own risk. Maybe it helps.

###

Make sure your Android Virtual Device Emulator has a Front and Back Camera set!
I had also the same error and removing and adding platforms did not work.

Sidenote: For the Emulator to work, you need to have him already open and then you could run your Debug APK from Android Studio 3.3 or run `ionic cordova run android`.

What worked: Open Android Virtual Device Manager (Android Studio -> Configure -> AVD Manager)

enter image description here

Actions -> Edit (Pencil Icon)

enter image description here

Show Advanced Settings

enter image description here

Make sure you did add a Front and Back camera! Before this setting was “None”, so this did not work and I got the errors above. After adding the camera, everything worked.

Hopefully this will help someone like me, who had an AVD configured without a camera.

Leave a Reply

Your email address will not be published. Required fields are marked *