gpt4 book ai didi

cordova - phonegap/cordova 从照片库文件传输中获取图片不起作用

转载 作者:行者123 更新时间:2023-12-02 01:47:36 24 4
gpt4 key购买 nike

我在组合 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

最佳答案

我目前的解决方法是删除“?”以及 imageUri 之后的所有内容。这样 URI 总是看起来像:

file:///mnt/sdcard/Android/data/com.XXX.YYY/cache/modified.jpg

在我的例子中,这可以正常工作并正确传输文件

关于cordova - phonegap/cordova 从照片库文件传输中获取图片不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24655467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com