Phonegap getPicture method fails when image file names with spaces in Android

旧巷老猫 提交于 2019-12-23 16:30:13

问题


I'm using the latest version of the phonegap camera plugin (i.e. 0.2.9) and 3.5 of phonegap Build, testing on Android, below is my code for getting 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 choose an image where its filename without spaces (e.g. test.jpg), the uploadPhoto method is called successfully, but when i choose an image where its filename with space (test 2.jpg), error occurs with the alert message "Unable to create bitmap".


回答1:


I have the same problem.My solution is update version to latest version(5.1.1).




回答2:


Use JavaScript encodeURI() Function to encode the path.

This will give you the file path in a format accepted by the standard specification. Still '(' and ')', which appears commonly on file names, are not handled by the function.

However, tailing the encodeURI function with 2 JavaScript String replace() Method to replace them with their RAW URL encoding equivalents should solve the issue.

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>


来源:https://stackoverflow.com/questions/25977116/phonegap-getpicture-method-fails-when-image-file-names-with-spaces-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!