我正在使用app,它允许用户将图像上传到服务器。图像来源可以是相机或来自图库,但在上传之前,图像将显示在应用
中现在,我的代码在从相机拍摄图像时工作正常,它在应用程序正文中显示,也会上传到服务器。但是,当我从图库中选择图像时,它既不会在应用中显示,也不会上传到服务器。
当我尝试在Phonegap论坛
中执行作为完整示例给出的代码时,发生了同样的事情http://docs.phonegap.com/en/2.5.0/cordova_camera_camera.md.html#camera.getPicture
我正在为Android开发此应用程序并使用Google Nexus 4进行测试,我在其他使用Jellybean,ICS和Gingerbread运行的手机上进行了检查,在这些操作系统上,相机和图库都正常运行。
这是我的JS代码:
function takePicture() {
navigator.camera.getPicture(
function(uri) {
var img = document.getElementById('camera_image');
img.style.visibility = "visible";
img.style.display = "block";
img.src = uri;
document.getElementById('camera_status').innerHTML = "Success";
},
function(e) {
console.log("Error getting picture: " + e);
document.getElementById('camera_status').innerHTML = "Error getting picture.";
},
{ quality: 10, destinationType: navigator.camera.DestinationType.FILE_URI});
}
/**
* Select picture from library
*/
function selectPicture() {
navigator.camera.getPicture(
function(uri) {
var img = document.getElementById('camera_image');
img.style.visibility = "visible";
img.style.display = "block";
img.src = uri;
alert(uri);
document.getElementById('camera_status').innerHTML = "Success";
},
function(e) {
console.log("Error getting picture: " + e);
document.getElementById('camera_status').innerHTML = "Error getting picture.";
},
{ quality: 10, destinationType: navigator.camera.DestinationType.FILE_URI, sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY});
}
/**
* Upload current picture
*/
function uploadPicture() {
// Get URI of picture to upload
var img = document.getElementById('camera_image');
var imageURI = img.src;
alert(img+" "+imageURI);
if (!imageURI || (img.style.display == "none")) {
document.getElementById('camera_status').innerHTML = "Take picture or select picture from library first.";
return;
}
// Verify server has been entered
server = document.getElementById('serverUrl').value;
if (server) {
// Specify transfer options
var options = new FileUploadOptions();
options.headers = {Connection : "close"}
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
options.chunkedMode = false;
// Transfer picture to server
var ft = new FileTransfer();
alert(img+" "+imageURI);
alert("server "+server);
ft.upload(imageURI, server, function(r) {
document.getElementById('camera_status').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded.";
}, function(error) {
document.getElementById('camera_status').innerHTML = "Upload failed: Code = "+error.code;
}, options);
}
}