简体   繁体   中英

Phonegap camera.getPicture issues with upload

I have two goals in my mobile application.

a) take picture with phone's camera and upload it to server
b) pick picture from phone's gallery and upload it to server

I have been using Phonegap and point "a" works well. Also "b".

Issue: it seems like every second upload fails. No matter if I start with point "a" or point "b". Or randomly combined.

Result:
1. success
2. failed
3. success
4. failed
4. etc.

Here is my code for point "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
    }
    );
}

and for point "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
    }
    );
}

Functions uploadPhotoCamera and uploadPhotoGallery are the same.

Here is Eclipse log:

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.

What could be wrong? Any suggestions appreciated.

It's not a problem about getPicture functions.

I've found the very simple solution.

In my opinion, the FileTransfer() of cordova plugin doesn't send the data about the end of file transfer to the server.

So, the server can't recognize the boundary of each file and, occurs the issue.

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

I think the fake upload will be let a server know the file transfer is the end.

Because, the Content-Length is 0.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM