ImageView from SQLite Database
The ImageView documentation says the image
property can be a URL string, file or blob… is it possible to display an image stored in a BLOB field in a SQLite database?
13 Answers
-
I haven't tried that yet but as far as I know that should work.
-
When I try to use this code to display the image:
Titanium.UI.createImageView({width:70, height:70, left:x, top:y, image:rows.fieldByName('imageData')});
I get this error:
[WARN] invalid image type. expected either TiBlob or TiFile, was: NSConcreteData in -[TiUIImageView setImage_:] (TiUIImageView.m:493)
How do I convert a SQLite BLOB to a
TiBlob
object? -
Try
rows.fieldByName('imageData').toBlob()
Hopefully that'll do it.
-
Thanks for the help Dan, but I'm getting an error when I try that:
[ERROR] Script Error = [<NSConcreteData 0x5882200> valueForUndefinedKey:]: this class is not key value coding-compliant for the key toBlob.
-
Hmm…what about if you try it like this?
rows.fieldByName('imageData').toImage()
-
Well, that doesn't throw any errors, but nothing is displayed on-screen… do I have to save the image into SQLite in a certain format?
-
Very interesting…technically I believe you should be able to do this, but I just tried it several different ways and no luck, I was not able to get the image to be displayed.
Would it be possible for you to instead save the image to the file system and store the path to it in your database?
-
Briley,
Where you able to resolve this issue? I'm trying to do the same thing, load an image stored in my database into an imageview.
-
I am trying to do the same thing. Any update on this question ?
many thanks ! -
the blob from sqlite needs to be converted into a byte array
Here is one solution to converting the image data
you could write a module to do it
-
Hi all,
Using the "database example using blob" data of the document API_DatabaseClass.pdf, I have tried debugging my own code. And finally, after several tries, I found out something weird which looks like a bug.
I have the feeling that the function 'fieldByName('image')' does not work when reading a Blob (at least an image in a Blob).
If you call 'field(x)', then it works fine.Then the image display in the ImageView works fine.
I hope this will help.
-
My 2 cents - don't store images in a database. Every time you do a query that selects those fields, the images will get loaded into memory and that can be problematic for many rows. The route I would advocate is storing the images on the local filesystem, and then reference the images by their path (a string column in your db).
-
why use image as BLOBE….in sqlite create a column name image and put the image directory on the field….like /resource/image/ex.jpg