我在结合 Cordova 的 camera.getPicture 插件和他们的 FileTransfer.upload 插件时遇到了一些问题。最奇怪的是,当我从相机拍摄照片时,一切都很好,而不是当我从图书馆检索照片时。
问题看起来像这样:Phonegap/Cordova Uploading Image from gallery is not working on Android Kitkat
但我使用的是 Cordova 3,所以必须解决 kitkat 问题。
我的代码:
var imageUploadUrl = setting_api_url + "Umbraco/Api/ImageApi/PostUpload";
var formSubmitUrl = setting_api_url + "Umbraco/Api/FormApi/PostSend";
var imageUriStorage = [];
var resultsCollection = [];
var FieldId;
var take_photo = function(fieldId) {
FieldId = fieldId;
navigator.camera.getPicture(onTakeSuccess,
onTakeFail,
{
quality: 30,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.CAMERA,
targetWidth: 800,
targetHeight: 800,
correctOrientation: true
});
};
var get_photo = function(fieldId) {
FieldId = fieldId;
navigator.camera.getPicture(onTakeSuccess, onTakeFail, {
quality: 30,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
targetWidth: 800,
targetHeight: 800,
correctOrientation: true });
};
var onTakeSuccess = function(imageURI) {
console.log("onTakeSuccess imageURI= " + imageURI);
var image = document.getElementById("preview-" + FieldId);
// UPLOAD PART COPIED
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = src.substr(src.lastIndexOf('/') + 1);
var filetype = src.substr(src.lastIndexOf('.') + 1);
if(filetype == "png") {
options.mimeType = "image/png";
}
else if (filetype == "jpg") {
options.mimeType = "image/jpeg";
}
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI(imageUploadUrl), image_upload_win, image_upload_fail, options);
// END.
imageUriStorage.push({ 'Id':FieldId, 'Path': imageURI});
image.src = imageURI;
};
var onTakeFail = function(message) {
alert('on take fail. Code: ' + message);
};
// called when upload succeeds
var image_upload_win = function (r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
};
// when upload fails
var image_upload_fail = function (error) {
alert("Code = " + error.code + "-" + error.http_status + ", image:" + error.source);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
};
take_photo 和 get_photo 都命中了相同的函数 onTakeSuccess,因此它们将获得相同的过程。当源是相机时,图像被正确上传,服务器给出正确的响应(和 HTTP 201)。
但是,当源是库时,这会失败。FileTransfer.Upload 给出错误代码 1(找不到文件),但这不可能是真的,因为图像在我的应用程序的图像元素中正确显示。
服务器给出 HTTP 400 错误请求。
这个过程有一个不同之处,就是图像 URL 的格式,但是这应该如何产生这种不同呢?
记录成功的 getPicture (CAMERA):
onTakeSuccess imageURI=file:///mnt/sdcard/Android/data/com.XXX.YYY/cache/1404910577158.jpg
Code = 201
Response = "8999"
Sent = 195
记录不成功的 getPicture (LIBRARY):
onTakeSuccess imageURI= file:///mnt/sdcard/Android/data/com.XXX.YYY/cache/modified.jpg?1404910903858
upload error source file:///mnt/sdcard/Android/data/com.XXX.YYY/cache/modified.jpg?1404910903858
upload error target https://CORRECTURL/Umbraco/Api/ImageApi/PostUpload
此外,getPicture (LIBRARY) 情况会发出此警报:
Code = 1-400, image:file:///mnt/sdcard/Android/data/com.XXX.YYY/cache/modified.jpg?1404910903858