Phonegap camera.getPicture上传问题

时间:2013-12-14 21:12:32

标签: javascript android mobile cordova

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

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

我一直在使用Phonegap并指出“a”效果很好。也是“b”。

问题:似乎每次上传都失败了。无论我从点“a”还是点“b”开始。或随机组合。

结果:
成功 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.

可能有什么不对?任何建议表示赞赏。

2 个答案:

答案 0 :(得分:2)

这对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。

答案 1 :(得分:1)

我在phonegap 2.9.0上面临同样的问题,在做了很多失败的试验来解决问题之后,我做了以下几点 - 我创建了一个新的phonegap 3.5.0项目 - 我从CLI构建了android平台 - 我将www文件夹复制到新项目根文件夹 - 我使用CLI添加了所有需要的插件 - 我在图标和启动画面的顶级config.xml文件中进行了必要的更改,因为文件构造不同 - 我运行了项目,现在没问题了。

相关问题