Android的照片上传到服务器最有效的方法 [英] Android uploading pictures to server in most efficient way

查看:265
本文介绍了Android的照片上传到服务器最有效的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到的图像连同其他数据(非常类似与attachements到电子邮件)到服务器。我还需要做的是在可靠的方式让我可以重试,等失败。

I need to get images along with other data (very similar to email with attachements) to the server. I also need to do it in reliable manner so I can retry, etc on failure.

服务器是WCF REST服务器和我做很多其他的通信与它(JSON),但是刚拿到这款新的要求上传图片。

Server is WCF REST server and I do lot of other communications with it(JSON) but just got this new requirement to upload images.

由于我使用JSON将数据发布到我的服务器 - 我用GSON在Android端序列化的数据。

Since I use JSON to post data to my server - I use GSON on Android side to serialize data.

下面是我如何得到它迄今实施的(一切以这种方式工作,但我刚开始用图片)

Here is how I got it implemented so far (everything else works this way but I just started with images)

  1. 用户填充的活动领域(文本数据)
  2. 在用户需要通过摄像头意图一些图片(S)。目前,我只是用1文件的图片
  3. 在我拍摄照片的SD卡,加载/调整它的大小 - dispaly上的ImageView和存储字节[]
  4. 在用户提交 - 我把所有的数据以及从byte []的图像,并把它转换成Java对象
  5. 在通话GSON转换器和序列化对象
  6. 保存对象到SQLite的
  7. 的AsyncTask寻找SQLite中的记录,打开光标并获取文本
  8. 的AsyncTask创建的HttpConnection和岗位文本数据到我的服务器。
  9. THE END

现在我的问题.. 显然,在#3 - 我爆炸公羊与我的字节数组。有时我甚至觉得我的Nexus S变得迟缓。但是,这样做 - 我避免填满SD卡或应用程序文件夹中有许多文件。我拍照,比抓住它。接下来的画面将覆盖previous之一。

Now to my problems.. Obviously on #3 - I "explode" ram with my byte arrays. Sometime I even feel my Nexus S becomes sluggish. But by doing that - I avoid filling SD card or app folder with many files. I take picture and than grab it. Next picture will overwrite previous one.

步骤#5是缓慢的。我没有尝试自定义序列上GSON和序列化,而不是字节数组成类似[1,-100,123,-12]我可以得到更小尺寸的Base64,但仍。这将是缓慢的。我可以有多达20幅图像...

Step #5 IS slow. I didn't try custom serializer on GSON and instead of serializing byte array into something like [1,-100,123,-12] I can get much smaller size with Base64 but still. It will be slow. And I can have up to 20 images...

步骤#6是没有问题的。但是,某些大小(我试过300像素的图像),我开始对OpenCursor步骤7误差

Step #6 is no problem. But with certain size (I tried 300px image) I started to get error in step 7 on OpenCursor

07-06 20:28:47.113: ERROR/CursorWindow(16292): need to grow: mSize = 1048576, size = 925630, freeSpace() = 402958, numRows = 2
07-06 20:28:47.113: ERROR/CursorWindow(16292): not growing since there are already 2 row(s), max size 1048576
07-06 20:28:47.113: ERROR/Cursor(16292): Failed allocating 925630 bytes for text/blob at 1,1

所以,这整个事情是不是我喜欢的东西。理想的情况是我想要的所有数据,以单件服务器进行上传。

So, this whole thing is not something I like. Ideally I want all data to be uploaded in single piece to server.

我在想时间戳,SD卡也许存储的图像和数据库存储只是他们的名字。比我发送给服务器之前,权将对其进行处理。而就成功,我会删除这些图片。这样的逻辑将SQLite的模式要复杂得多,但也许没有更好的办法?!

I was thinking maybe storing images timestamped on SD card and store only their name in DB. Than I would process them right before sending to server. And on success I would delete those images. This kind of logic will make SQLite schema much more complex but maybe there is no better way?!

我想我在寻找处理图像的最佳实践。如何做到以最小的内存/ CPU使用率跟随着:

I guess I'm looking for best practice to deal with images. How to do followin with minimal memory/CPU usage:

  1. 拍照
  2. 显示缩略图
  3. 调整
  4. 发送到服务器

修改1:

上传整个shizang作为多部分MIME消息目前我正在研究的可能性。这就需要增加一些JAR的到我的Andr​​oid包。此外,我不知道如何有效地将Apache的code加载图像,并将其发送(我想比我的code更好) <一href="http://okandroidletsgo.word$p$pss.com/2011/05/30/android-to-wcf-streaming-multi-part-binary-images/">http://okandroidletsgo.word$p$pss.com/2011/05/30/android-to-wcf-streaming-multi-part-binary-images/

Currently I'm researching possibility of uploading whole shizang as a multi-part MIME message. That would require adding some JAR's to my Android package. Also I'm not sure how effective will be Apache code to load images and sending them(I guess better than my code) http://okandroidletsgo.wordpress.com/2011/05/30/android-to-wcf-streaming-multi-part-binary-images/

和我将必须处理解析这一切的WCF一边,因为没有办法使用内置的.NET框架。做到这一点

And that I would have to deal with parsing all this on WCF side since there is no way to do it with built-on .NET framework.

<一个href="http://ants$c$c.blogspot.com/2009/11/parsing-multipart-form-data-in-wcf.html">http://ants$c$c.blogspot.com/2009/11/parsing-multipart-form-data-in-wcf.html

请告诉我,如果你尝试这样做!

PLEASE TELL ME IF YOU TRIED THIS!

编辑2:

MIME也是白搭。是毫无意义的,因为它使用的Base64这是同样的事情二进制序列化。

MIME is no good. There is no point since it serializes binary using Base64 which is same thing..

推荐答案

没有人回答,但这里是我想通硬的方式:

Nobody answered but here is what I figured hard way:

规则#1:当与图像处理 - 避免使用对象/内存。听起来很明显,但事实并非如此。我计算过,调整图像为800x600正常。任何更大的 - 你可以考虑仅仅把它当作是因为它是可以做到的HTTP数据流上的大文件,但它很难与OOM异常工作的时候你载入图片到内存中以便处理

Rule #1: When dealing with images - avoid using objects/memory. Sounds obvious but it's not. I figured that resizing image to 800x600 is OK. Anything bigger - you may consider just leaving it as is because it is possible to do http stream on bigger file but it's hard to work with OOM exceptions when you load images into memory for processing

规则#2:当使用GSON - 使用JsonWriter填充流。否则内存就会爆炸。比起传递流到HttpClient的。 JsonWriter会写在块和数据将被发送,因为它的过程。

Rule #2: When use GSON - use JsonWriter to populate stream. Otherwise memory will explode. Than pass that stream into HttpClient. JsonWriter will write in chunks and data will be sent as it process.

规则#3:请参见规则#2。它将为多个小图像确定。这样GSON将由一个序列化到1送入流。每幅图像将被加载反正INT内存。

Rule #3: See rule #2. It will work OK for multiple small images. This way GSON will serialize them 1 by one and feed into stream. Each image WILL be loaded int memory anyway.

规则#4:这可能是最好的解决办法,但需要与服务器进行更多协调。图片发送1:1向服务器发送消息之前。他们将作为流不进行任何编码。这种方式,他们不必采用base64烯codeD和他们没有在存储器设备上被加载。传输的尺寸将会更小为好。当发送的所有图片 - 发布信息的主要对象,同时收集所有的包放在服务器

Rule #4: This is probably the best solution but requires more coordination with server. Images sent 1 by 1 before message sent to server. They sent as stream without any encoding. This way they don't have to be base64 encoded and they don't have to be loaded in memory on device. Size of transmission will be smaller as well. When all images sent - post main informational object and collect all package together on server.

规则#5:忘掉存储BLOB SQLite中

Rule #5: Forget about storing BLOB in SQLite

底线:

  • 这是资源的长期便宜很多,没有任何调整大小发送图像。调整大小才有意义,只有当图像获得的约800×600杂交
  • 在单个封装中发送多张图片是有道理的,当图像获取的小象600x400杂交
  • 当你需要上传的文件 - 开始思考溪流随处可见。不加载的东西到内存中。

这篇关于Android的照片上传到服务器最有效的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆