【问题标题】:Phonegap getPicture method fails when image file names with spaces in Android当Android中带有空格的图像文件名时,Phonegap getPicture方法失败
【发布时间】:2014-09-22 14:55:39
【问题描述】:

我正在使用最新版本的phonegap相机插件(即0.2.9)和phonegap Build 3.5,在Android上测试,下面是我获取图像的代码:

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 }
);

当我选择文件名不带空格的图像(例如 test.jpg)时,uploadPhoto 方法被成功调用,但是当我选择文件名带空格的图像(test 2.jpg)时,警报会发生错误消息“无法创建位图”。

【问题讨论】:

  • 我认为您需要 URLEncode 文件名才能正常工作,因为按照 URL 标准规范不接受空格。
  • 但是要编码哪个部分?我尝试了destinationType:encodeURI(navigator.camera.DestinationType.FILE_URI),仍然无法工作@frank
  • 您需要对文件名进行编码(包含空格的字符串)。
  • @VeniceLeung 你好,威尼斯,你能解决这个问题吗?我遇到了完全相同的问题,似乎是 phonegap/cordova 中的一个错误。谢谢!
  • 我也遇到了同样的问题,你解决了吗?

标签: android cordova phonegap-plugins phonegap-build


【解决方案1】:

使用 JavaScript encodeURI() 函数对路径进行编码。

这将为您提供标准规范接受的格式的文件路径。函数仍然不处理通常出现在文件名上的 '(' 和 ')'。

但是,使用 2 个 JavaScript String replace() 方法 尾随 encodeURI 函数以将它们替换为它们的 RAW URL 编码等效项应该可以解决问题。

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>

【讨论】:

    【解决方案2】:

    我的解决方案是更新到最新版本 (5.1.1)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多