我在使用Cordova的camera.getPicture插件和它們的FileTransfer.upload插件時遇到了一些問題。最奇怪的是,當我從相機拍攝照片時,一切運作良好,而不是從圖書館檢索時。phonegap/cordova getPicture from photolibrary file-transfer not working
的問題是這樣的一個:Phonegap/Cordova Uploading Image from gallery is not working on Android Kitkat
但是我用科爾多瓦3,這樣就必須解決奇巧的問題。
我的代碼:
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錯誤請求。
這個過程有一個不同之處,就是圖片網址的格式,但是應該如何改變呢?
登錄成功Getpicture中(CAMERA):
onTakeSuccess imageURI=file:///mnt/sdcard/Android/data/com.XXX.YYY/cache/1404910577158.jpg
Code = 201
Response = "8999"
Sent = 195
登錄不成功Getpicture中(庫):
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中(庫)的情況給出了這樣的警告:
Code = 1-400, image:file:///mnt/sdcard/Android/data/com.XXX.YYY/cache/modified.jpg?1404910903858
您有這方面的任何解決方案?我有同樣的問題,從我的圖片庫的路徑是不同於imageURI – fsi
還沒有找到它。 –
也許這個答案可能是有用的http://stackoverflow.com/questions/26154810/how-to-get-image-object-using-cordova – fsi