Phonegap camera.get上传图片时出现问题

Phonegap camera.getPicture issues with upload

本文关键字:问题 camera get Phonegap      更新时间:2023-09-26

我的移动应用程序有两个目标。

a) 用手机的相机拍照并上传到服务器
b)从手机的图库中挑选照片并上传到服务器

我一直在使用Phonegap,点"a"效果很好。也作"b"。

问题:似乎每秒钟上传都会失败。不管我是从点"a"还是从点"b"开始。或者随机组合。

结果:
1.成功
2.失败
3.成功
4.失败
4.等

这是我对点"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.

可能出了什么问题?欢迎提出任何建议。

这不是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

我认为假上传会让服务器知道文件传输已经结束。

因为,"内容长度"为0。

我在phonegap 2.9.0上遇到了同样的问题,在做了许多失败的尝试来解决这个问题后,我做了以下操作-我创建了一个新的phonegap 3.5.0项目-我从CLI构建了android平台-我将www文件夹复制到新的项目根文件夹-我使用CLI添加了所有需要的插件-我对图标和启动屏幕的顶级config.xml文件进行了必要的更改,因为文件结构不同-我负责这个项目,现在一切正常。