【问题标题】:Phonegap camera.getPicture issues with uploadPhonegap camera.getPicture 上传问题
【发布时间】:2013-12-14 21:12:32
【问题描述】:

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

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.

可能出了什么问题?任何建议表示赞赏。

【问题讨论】:

标签: javascript android mobile cordova


【解决方案1】:

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。

【讨论】:

  • 这也可以,我同意。我的解决方案:发生错误时,我再次调用 ft.upload。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    相关资源
    最近更新 更多