How to select multiple images in Cordova / Phonegap

My current job allows me to develop mobile apps -- using Cordova.

I came to a blocker when I needed to allow users to select multiple images. This is how I did it:

Install this: https://www.npmjs.com/package/cordova-plugin-telerik-imagepicker

And the trickiest part is instead of using their own requestReadPermissions function. Use something like that from here: https://www.npmjs.com/package/cordova-plugin-android-permissions

The full code:


function onDeviceReady() {
	function hasReadPermission() {
		window.imagePicker.hasReadPermission(
			function(result) {
				if (result == false) {
				     requestReadPermission();
				} else {
					window.imagePicker.getPictures(
						function(results) {
							for (var i = 0; i < results.length; i++) {
								Entries.uploadPhoto("data:image/jpeg;base64," + results\[i]);
							}
						}, function (error) {
							console.log('Error: ' + error);
						}, {
							outputType: 1, // 1 = base64 string output
							quality: 50,
							width: 600,
							height: 600,
						}
					);
				}
			}
		)
	}
				
function requestReadPermission() {
	var permissions = cordova.plugins.permissions;
	permissions.requestPermission(permissions.READ_EXTERNAL_STORAGE, success, error);

	function error() {
		console.warn('Camera permission is not turned on');
	}

	function success( status ) {
		if( !status.hasPermission ) {
			console.log('no permission')
		} else {
			hasReadPermission();
		}
	}
}

hasReadPermission();

}

document.addEventListener('deviceready', onDeviceReady);

Leave any comments if you liked it, helped you or if you have any questions

Rock on!

Comments