Phonegap getPicture method doesn't work when image file names with spaces in Android
I am using the latest phonegap camera plugin (i.e. 0.2.9) and 3.5 phonegap Build, testing on Android, below is my code to get images:
navigator.camera.getPicture(uploadPhoto,
function(message) { console.log('get picture failed');alert(message) },
{ quality: 50, targetWidth: 400, targetHeight: 400,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY }
);
when I select an image where its name is without spaces (for example test.jpg) the uploadPhoto method is called successfully, but when I select an image where its file name is with a space (test 2.jpg) an error with a warning message "Unable to create bitmap ".
source to share
Use JavaScript encodeURI () Function to encode the path.
This will give you the path to the file in the format accepted by the standard specification. Still the '(' and ')' that usually appears in filenames is not handled by the function.
However, the fix is to use the encodeURI function by using the JavaScript String replace () method to replace them with RAW URL encoding equivalents.
var success = function(path) {
var urlEncodedPath = encodeURI(path)
.replace('(', '%28')
.replace(')', '%29');
document.getElementById("demo").innerHTML = urlEncodedPath;
// do something
};
// Actual success callback from cordova camera
// navigator.camera.getPicture(success, fail, options);
// An Example for demonstating in browser
success('file:///storage/emulated/0/Android/data/com.your.app/cache/hd wallpaper nature(1).jpg');
<p id="demo" style="word-wrap: break-word;"></p>
source to share