android - How can I save image in my Sugar ORM database? -


i want save image in database sugar orm. how can this?

i have next code try save it:

entity:

import android.graphics.drawable.drawable; import android.media.image;  import com.orm.sugarrecord; import com.orm.dsl.notnull; import com.orm.dsl.unique;  public class exercise extends sugarrecord {      @unique     protected string name;     @notnull     protected string description;     protected drawable image;     protected string video;      public exercise() {     }      public exercise(string name, string description, drawable image, string video) {         this.name = name;         this.description = description;         this.image = image;         this.video = video;     }      /**      * @return string name      */     public string getname() {         return name;     }      /**      * @param name      */     public void setname(string name) {         this.name = name;     }      /**      * @return string description      */     public string getdescription() {         return description;     }      /**      * @param description      */     public void setdescription(string description) {         this.description = description;     }      /**      * @return image image      */     public drawable getimage() {         return image;     }      /**      * @param image      */     public void setimage(drawable image) {         this.image = image;     }      /**      * @return string video      */     public string getvideo() {         return video;     }      /**      * @param video      */     public void setvideo(string video) {         this.video = video;     } } 

and trying save next code:

 exercise addexercise = new exercise("prueba", "prueba 2", image.getdrawable(),"");     addexercise.save(); 

variable image imageview, , image save image taken in gallery:

if(requestcode == select_photo && resultcode == getactivity().result_ok && null != data)         {             uri selectedimage = data.getdata();             string[] filepathcolumn = {mediastore.images.media.data};             cursor cursor = getactivity().getcontentresolver().query(selectedimage, filepathcolumn, null, null, null);             cursor.movetofirst();             int columnindex = cursor.getcolumnindex(filepathcolumn[0]);             string picturepath = cursor.getstring(columnindex);             cursor.close();             image.setimagebitmap(bitmapfactory.decodefile(picturepath));         } 

when try save appear next error:

class cannot read sqlite3 database. please check type of field image(android.graphics.drawable.drawable)

thank you!

try this

define

protected byte[] image;  

in exercise class , change type of image variable in constructor. convert image byte array

  bytearrayoutputstream stream=new bytearrayoutputstream();    bitmap.compress(bitmap.compressformat.png,90,90,stream);    byte[] image_byte=stream.tobytearray(); 

actually storing entire image in db bad idea based on read/write efficiency.it better store image in seperate folder , store path string(varchar) in database


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -