|
Post by egghead on Dec 9, 2016 6:49:19 GMT
I followed the docs here for Camera.choosePicture : def select_picture_from_gallery Rho::Camera.choosePicture({}, url_for(:action => :picture_taken_callback)) end def picture_taken_callback # Did we really take a picture? if (@params["status"]=="ok") # If so, show it @img = Rho::Application.expandDatabaseBlobFilePath(@params["imageUri"]) Rho::WebView.navigate(url_for(:action => :show_picture, :query => {:image => @img})) else # Otherwise we are done here Rho::WebView.navigate(url_for(:action => :index)) end end But the image chooser doesn't open up in Rhosimulator. In device (Android), image chooser is opening, but after choosing the picture, the image is not shown. Only the label is shown (Image) My show_picture.erb is : <div data-role="page" data-add-back-btn="false"> <div data-role="content"> <ul data-role="listview"> <li> <div class="itemLabel">Image</div> <div class="itemValue"><img src="<%= @img %>" /></div> </li> </ul> </div> </div> Device log is shown with error in attached file below :
|
|
|
Post by Dmitry Soldatenkov on Dec 9, 2016 11:01:37 GMT
Hi,
1. Rho::Camera.choosePicture() should return full file path on device in @params["imageUri"]. So you should not make full path by Rho::Application.expandDatabaseBlobFilePath() (actually now it return empty path - this is root of issue)
2. When you call Rho::Camera.choosePicture(), Rhodes copy selected image to Application's db blob path. So in @params["imageUri"] you have full file path and it path in application's db/blob folder like this - /data/data/com.rhomobile.rhodessystemapisamples/rhodata/db/db-files/IMG_20161209_114739.jpg. This path can be used it in WebView (in <img> etc.) without transform. But if you want to use this image file in your DB, your should transform path to local path by this code :
blob_path = Rho::Application.relativeDatabaseBlobFilePath(@params['imageUri'])
3. So in your case you should just use @params["imageUri"] without convert. P.S. If this information not help you - describe issue more detailed : Android version, what is value in @params["imageUri"] ( add this line to callback code -> puts 'IMAGE URI IN CALLBACK = '+@params["imageUri"])?
|
|
|
Post by Vladimir Musulainen on Dec 9, 2016 11:32:05 GMT
egghead, you can find JS code that shows choosen picture at KitchensinkJS . There is js code, but the idea is clear
|
|
|
Post by egghead on Dec 13, 2016 10:40:08 GMT
Thank you Dmitry, Vladimir for quick response.
I have changed my choosePicture method :
def update Rho::Camera.outputFormat = 'dataUri' Rho::Camera.choosePicture({}, url_for(:action => :picture_taken_callback)) end
and my callback :
def picture_taken_callback if (@params["status"]=="ok") @custodian = Custodian.find(:first, :conditions =>{'custodian_id'=> $custid}) @custodian.update_attributes({'image' => @params['imageUri']}) elsif (@params["status"]=="cancel") #user cancelled operation # do nothing else Alert.show_popup('Error occurred!') end WebView.navigate(url_for(:action => :index)) end
and now it works in device. Picture is also showing in device. However, in backend, instead of Base64 string, the picture is stored like this :
/data/user/0/com.rhomobile.pamimageupload/rhodata/db/db-files/IMG_20161213_032750.jpg
The image chooser still does not open in RhoSimulator
My takePicture method, however, works flawlessly, image is captured and stored as Base64 in device and also synced as Base64 in backend :
data:image/jpeg;base64,/9j/4UFTRXhpZgAATU0AKgAAAAgACAEPAAIAAAAHAAAAbgEQAA <snipped for brevity sake>
Here is my takePicture method which calls same callback as choosePicture :
def new if !defined? Rho::Camera Alert.show_popup('Camera is not supported on this platform!') render :action => :index else cameras = Rho::Camera.enumerate if cameras.length == 0 Alert.show_popup('No camera!') render :action => :index else camera = cameras[0] #Use default camera camera.outputFormat = 'dataUri' camera.takePicture({}, url_for(:action => :picture_taken_callback)) end end end
Here's my show.erb :
<li> <div class="itemLabel">Image</div> <div class="itemValue"><img src="<%= @custodian.image %>" width="300px" /></div> </li>
I use generated model file without modification :
Model file (custodian.rb) -------------------------
# The model has already been created by the framework, and extends Rhom::RhomObject # You can add more methods here class Custodian include Rhom::PropertyBag
# Uncomment the following line to enable sync with Custodian. enable :sync
#add model specific code here end
So, why is takePicture generating the Base64 string for the image and choosePicture does not?
|
|
|
Post by Vladimir Musulainen on Dec 13, 2016 16:08:12 GMT
egghead , about RhoSilmulator. RhoSimulator supports basic APIs like Application, Network, Database e.g. And it does not support APIs that are related to a devices: Camera, Sensor e.g. In general, if an API is supported on WIndows Desktop (WIN32) it means this API is supported on RhoSimulator under windows and macos.
|
|
|
Post by egghead on Dec 14, 2016 1:46:41 GMT
I don't expect takePicture to work in Rhosimulator, but choosePicture should work. At least in old Rhodes it was working (it was choose_picture). Anyway, that's not my primary concern now (I should have rephrased my thread topic). My primary concern is why with outputFormat set to dataURI, choosePicture is not giving me the Base64 string of the image and giving me the path to the blob instead while takePicture is giving me the string correctly.
Dmitry has already explained that Rho::Camera.choosePicture() should return full file path on device in @params["imageUri"]. But I used Camera.output format='dataURI' before calling choosePicture, but it doesn't seem to give me the base64 string.
Obviously, there's something wrong with my code above.
Would be thankful if anyone can point out what I'm doing wrong. Basically I need the base64 representation of the image to be stored in device and back end SQL server database field (when synced). For that,my takePicture code works. But choosePicture doesn't. It instead stores the path to the blob file in my SQL Server backend which I don't require. I require the base64 representation of the image. I expected dataURI to convert the image to base64 string, I don't understand why it works in takePicture but not choosePicture. Hope I'm making myself clear.
|
|
|
Post by Dmitry Soldatenkov on Dec 14, 2016 2:29:23 GMT
I don't expect takePicture to work in Rhosimulator, but choosePicture should work. At least in old Rhodes it was working (it was choose_picture). Anyway, that's not my primary concern now (I should have rephrased my thread topic). My primary concern is why with outputFormat set to dataURI, choosePicture is not giving me the Base64 string of the image and giving me the path to the blob instead while takePicture is giving me the string correctly. Dmitry has already explained that Rho::Camera.choosePicture() should return full file path on device in @params["imageUri"]. But I used Camera.output format='dataURI' before calling choosePicture, but it doesn't seem to give me the base64 string. Obviously, there's something wrong with my code above. Would be thankful if anyone can point out what I'm doing wrong. Basically I need the base64 representation of the image to be stored in device and back end SQL server database field (when synced). For that,my takePicture code works. But choosePicture doesn't. It instead stores the path to the blob file in my SQL Server backend which I don't require. I require the base64 representation of the image. I expected dataURI to convert the image to base64 string, I don't understand why it works in takePicture but not choosePicture. Hope I'm making myself clear. Hi, I see - when you set any properties in Camera like this: Rho::Camera.outputFormat = 'dataUri' It is equivalent of this : camera = Rho::Camera.getDefault camera.outputFormat = 'dataUri' and take effect only to default Camera object for takePicture() not for static method choosePicture(). So if you want to setup choosePicture() you should setup properties when you call choosePicture like this: Rho::Camera.choosePicture({:outputFormat => 'dataUri'}, url_for(:action => :picture_taken_callback))
|
|
|
Post by egghead on Dec 14, 2016 4:55:22 GMT
Problem resolved. Thanks, Dmitry
|
|