我正在我的应用程序中实现上传图像到服务器功能。 用户可以通过相机和相册两种方式上传图像。
我审阅了这些链接,并使用了该代码。 http://docs.phonegap.com/en/2.9.0/cordova_media_capture_capture.md.html#Capture http://docs.phonegap.com/en/2.9.0/cordova_file_file.md.html#FileTransfer
主要问题是, 在第一种情况下,当我打开相机时,应用程序暂停,甚至不捕获图像。
在第二种情况下,当我打开照片库时,应用程序暂停,但在这种情况下,我成功将视频上传到服务器。 完成此操作后,应用程序将恢复,并加载索引页面。 我使用的是cordova3.6.4版本。
function captureImage() {
navigator.device.capture.captureImage(captureSuccess, captureError, {limit: 1});
}
function captureSuccess(mediaFiles) {
alert("###1");
var i, len;
for (i = 0, len = mediaFiles.length; i < len; i += 1) {
uploadFile(mediaFiles[i]);
}
}
function uploadFile(mediaFile) {
alert("###2");
var ft = new FileTransfer(),
path = mediaFile.fullPath,
name = mediaFile.name;
alert("image path "+path);
ft.upload(path,
"http://my.domain.com/upload.php",
function(result) {
console.log('Upload success: ' + result.responseCode);
console.log(result.bytesSent + ' bytes sent');
},
function(error) {
alert("error ###3");
console.log('Error uploading file ' + path + ': ' + error.code);
},
{ fileName: name });
}
图片来自PhotoGallery
function photoAlbum() {
// Retrieve image file location from specified source
navigator.camera.getPicture(
uploadPhoto,
function(message) { alert('get picture failed'); },
{
quality : 50,
destinationType : navigator.camera.DestinationType.FILE_URI,
sourceType : navigator.camera.PictureSourceType.PHOTOLIBRARY
}
);
}
function uploadPhoto(imageURI) {
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
var params = {};
params.value1 = "test";
params.value2 = "param";
options.params = params;
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI("http://some.server.com/upload.php"), win, fail, options);
}
我在logcat中收到警告
05-25 17:11:40.534: W/PluginManager(27869): THREAD WARNING: exec() call to Capture.captureImage blocked the main thread for 51ms. Plugin should use CordovaInterface.getThreadPool().
05-25 17:11:40.564: D/CordovaActivity(27869): Paused the application!
05-25 17:11:40.564: D/CordovaWebView(27869): Handle the pause
05-25 17:11:40.564: D/Socket_Pool(27869): Failed to create TCP Fin Aggregation interface.
05-25 17:11:40.564: D/Socket_Pool(27869): netstack: CloseUnusedSockets is ON
05-25 17:11:40.564: D/Socket_Pool(27869): netstack: system net.statistics value: 0
05-25 17:11:40.564: D/Socket_Pool(27869): Failed to create TCP Fin Aggregation interface.
05-25 17:11:40.564: D/Socket_Pool(27869): netstack: CloseUnusedSockets is ON
05-25 17:11:40.564: D/Socket_Pool(27869): netstack: system net.statistics value: 0
案例2 logcat警告
05-25 17:51:23.964: W/PluginManager(28392): THREAD WARNING: exec() call to Camera.takePicture blocked the main thread for 25ms. Plugin should use CordovaInterface.getThreadPool().
05-25 17:51:23.994: D/CordovaActivity(28392): Paused the application!
05-25 17:51:23.994: D/CordovaWebView(28392): Handle the pause
05-25 17:51:24.004: D/Socket_Pool(28392): Failed to create TCP Fin Aggregation interface.
05-25 17:51:24.004: D/Socket_Pool(28392): netstack: CloseUnusedSockets is ON
05-25 17:51:24.004: D/Socket_Pool(28392): netstack: system net.statistics value: 0
05-25 17:51:24.004: D/Socket_Pool(28392): Failed to create TCP Fin Aggregation interface.
05-25 17:51:24.004: D/Socket_Pool(28392): netstack: CloseUnusedSockets is ON
05-25 17:51:24.004: D/Socket_Pool(28392): netstack: system net.statistics value: 0