1

My Java server is receiving a Canvas object from an Android application, and I am trying to send this object out to the other apps. Is it possible to send this without the server having any knowledge of what the object contains, and just simply forward the object without storing it?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Luke1111
  • 57
  • 1
  • 4
  • umm... `RequestDispatcher#forward(request, response)` does not suit your need? – Nishant Mar 19 '12 at 16:43
  • 1
    if you mean send any object over a socket - does not work unless you can [serialize](http://en.wikipedia.org/wiki/Serialization#Java) it. Android uses [parcelable](http://developer.android.com/reference/android/os/Parcelable.html) for that internally when doing ipc – zapl Mar 19 '12 at 16:49

1 Answers1

2

Canvas is not serializable, so you won't be able to do that.

http://developer.android.com/reference/android/graphics/Canvas.html

There is private state in Canvas objects that you will not be able to capture, even if you write your own serialization scheme.

You will need to think about some other data structure that is available you. For example, a bitmap can be encoded and written to a stream, and therefore transferred over the network.

Here's a post that talks about saving a canvas into a bitmap, and then into a jpg,

Image on canvas to JPEG file

As for forwarding without storing, you can store whatever you want in memory, so that's not a problem. You do need to think about the load on the server. If you run into a situation where you have lots of image data in memory. That will exhaust the resources on your server.

As for sending to other apps, you will need to send a C2DM push message to the target device / app that says "there's something for you on the server, come and get it". For example, when one app puts the data on the server, there's an ID with it. The C2DM includes this ID, so the target apps do a GET with that ID to retrieve the associated image. Note that the image data will be hanging around in memory for an indefinite amount of time, as there's no guarantee when or if the C2DM push will get to all the target devices. It might make sense to write the data to a persistent store (like a database or file system), if only temporarily.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • In which case would it be possible to send an object containing both path and paint, which could then be used to draw onto the canvas at the other end? – Luke1111 Mar 19 '12 at 17:50
  • neither path nor paint are serializable either. i don't know your app, so you will need to look at what exactly you need to capture to restore what you need on the other side. while path and paint are not serializable, if you can get what you want from their getters, then you can build your own "transfer" object that is serializable and can be used to re-construct what you need on the other side. – Jeffrey Blattman Mar 19 '12 at 18:55