Image Uploading With Rhodes, RhoConnect & Rails3

Visnupriya

1 min read

Recently I working on an app project which involves uploading and transferring images from smartphones to Web and vice-versa. My stack was Rhodes, RhoConnect, Rails 3. Thought I would share pieces of code snippets that would help you get this all integrated in your RhoMobile apps.

Lets say we have an App that got Product model with some attributes along with product image.
Rails part:
Used the famous Paperclip gem and while exposing the API to RhoConnect, I used the base64.encode64 method.
[source language=”ruby”]
require ‘base64’
product= Product.new(params[:product])
product.save
if product.image_file_name != nil
product.update_attributes({:encoded_image => Base64.encode64(open(product.image.path,”rb”) { |io| io.read })})
end
[/source]
Rhodes part:
Enabled the camera capability to upload picture or take new picture.
[source language=”ruby”]
capabilities:
– camera
[/source]
Sample Take picture code from Rhodes controller
[source language=”ruby”]
def take_picture
width =100
height =100
settings = {:desired_width => width, :desired_height => height }
Camera::take_picture(url_for(:action => :camera_callback), settings)
redirect :action => :wait
end
[/source]
Sample Choose picture code from Rhodes controller
[source language=”ruby”]
def choose_picture
width =100
height =100
settings = {:desired_width => width, :desired_height => height }
Camera::take_picture(url_for(:action => :camera_callback), settings)
redirect :action => :wait
end
[/source]
Now the key thing is to encode the image and store it in image_uri attribute while camera_callback is called
[source language=”ruby”]
require ‘base64’
def camera_callback
imagebase64= Base64.encode64(open(Rho::RhoApplication::get_blob_path(@params[‘image_uri’])) { |io| io.read }).gsub(/\r/,””).gsub(/\n/,””)
@product=Product.create({:image_uri => imagebase64})
end
[/source]
RhoConnect part
Generate the RhoConnect model called Product which by default have several methods.
The code snippet from query method (check the image_uri hash)
[source language=”ruby”]
def query(params=nil)
@products=JSON.parse(RestClient.get(“http://localhost:3000/products.json”).body)
@result={}
@products.each do |product|
@result[product[“id”].to_s] = {:name => product[‘name’],:image_uri => product[‘encoded_image’]}
end if @products
@result
end
[/source]
The code snippet from create method
[source language=”ruby”]
def create(create_hash)
url = “http://localhost:3000/products.json”
product = RestClient.post( url, create_hash.to_json, :content_type => ‘application/json’)
end
[/source]
Let me know if you want me to publish a simple Android app on Google Play so that you can actually play with the whole application.

Related posts:

One Reply to “Image Uploading With Rhodes, RhoConnect & Rails3”

  1. I’ve come across this post, because I’m doing an image upload over bluetooth … Base64 encoding the image first, and than sending the data …
    I actually use Base64.strict_encoding, thus not requiring the gsubs … but my problem is, that the base64_string somehow gets chopped at random points. Or at least, that’s what puts #{base64_string} shows me in debug output.
    And I can’t have that happening, because it breaks the command-statemachine on my receiver end.
    I’ve tried the exact same code in plain ruby, and there it works without getting chopped up. What could be issue here ?
    grtz
    laerg

Leave a Reply

Your email address will not be published. Required fields are marked *