我在我的移动应用程序中有两个目标。
a)用手机摄像头拍照上传到服务器
b)从手机的图库中挑选图片并上传到服务器
我一直在使用Phonegap和点"a“,效果很好。也叫"b“。
问题:似乎每秒上传都会失败。不管我是从"a“点还是从"b”点开始。或者随机组合。
结果:
这是我对“a”点的代码:
function getImageCamera() {
navigator.camera.getPicture(uploadPhotoCamera, function(message) {
},{
targetWidth: 1200,
targetHeight: 900,
quality: 75,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.CAMERA
}
);
}对于点"b":
function getImageGallery() {
navigator.camera.getPicture(uploadPhotoGallery, function(message) {
},{
targetWidth: 1200,
targetHeight: 900,
quality: 75,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.SAVEDPHOTOALBUM
}
);
}函数uploadPhotoCamera和uploadPhotoGallery是相同的。
以下是Eclipse日志:
12-14 22:44:15.303: W/FileTransfer(32613): Error getting HTTP status code from connection.
12-14 22:44:15.303: W/FileTransfer(32613): java.io.EOFException
12-14 22:44:15.303: W/FileTransfer(32613): at libcore.io.Streams.readAsciiLine(Streams.java:203)
12-14 22:44:15.303: W/FileTransfer(32613): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:573)
12-14 22:44:15.303: W/FileTransfer(32613): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:821)
12-14 22:44:15.303: W/FileTransfer(32613): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
12-14 22:44:15.303: W/FileTransfer(32613): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:495)
12-14 22:44:15.303: W/FileTransfer(32613): at org.apache.cordova.FileTransfer$1.run(FileTransfer.java:484)
12-14 22:44:15.303: W/FileTransfer(32613): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-14 22:44:15.303: W/FileTransfer(32613): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-14 22:44:15.303: W/FileTransfer(32613): at java.lang.Thread.run(Thread.java:856)
12-14 22:44:15.311: E/FileTransfer(32613): {"target":"http:\/\/some_IP\/api_upload.php","source":"file:\/\/\/storage\/emulated\/0\/Android\/data\/com.tisamobile\/cache\/resize.jpg?1387057455160","http_status":0,"code":3}
12-14 22:44:15.311: E/FileTransfer(32613): java.io.EOFException
12-14 22:44:15.311: E/FileTransfer(32613): at libcore.io.Streams.readAsciiLine(Streams.java:203)
12-14 22:44:15.311: E/FileTransfer(32613): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:573)
12-14 22:44:15.311: E/FileTransfer(32613): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:821)
12-14 22:44:15.311: E/FileTransfer(32613): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
12-14 22:44:15.311: E/FileTransfer(32613): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:495)
12-14 22:44:15.311: E/FileTransfer(32613): at org.apache.cordova.FileTransfer$1.run(FileTransfer.java:484)
12-14 22:44:15.311: E/FileTransfer(32613): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-14 22:44:15.311: E/FileTransfer(32613): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-14 22:44:15.311: E/FileTransfer(32613): at java.lang.Thread.run(Thread.java:856)
12-14 22:44:15.311: E/FileTransfer(32613): Failed after uploading 103425 of 103425 bytes.会出什么问题呢?如有任何建议,欢迎光临。
发布于 2013-12-17 15:26:19
这不是getPicture函数的问题。
我已经找到了非常简单的解决方案。
在我看来,cordova插件的FileTransfer()不会将文件传输结束的数据发送到服务器。
因此,服务器无法识别每个文件的边界,并出现问题。
var url = "http://www.upload-server-domain";
var imgFileURI = '/filepath/test.jpg';
var ft = new FileTransfer();
ft.upload('', encodeURI(url), null, null, null); // SOLUTION : fake upload
ft.upload(imgFileURI, encodeURI(url), fnSuccessCallback, fnErrorCallback, options); // real upload我认为假上传会让服务器知道文件传输结束了。
因为,Content-Length是0。
发布于 2014-08-17 19:13:51
我在PhoneGap2.9.0上遇到了同样的问题,在做了许多失败的试验来解决这个问题后,我做了以下事情--我创建了一个新的PhoneGap3.5.0项目--我从命令行界面构建了安卓平台--我复制了www文件夹到新的项目根文件夹--我使用命令行界面添加了所有需要的插件--我在顶层的config.xml文件中对图标和闪屏做了必要的修改,因为文件结构不同--我运行了这个项目,现在一切都好了。
https://stackoverflow.com/questions/20588326
复制相似问题