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 の形式という違いが 1 つありますが、それによってどのように違いが生じるのでしょうか。
成功した 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