2016-05-28 1 views
3

私はsugar ormを使ってデータベースに画像を保存します。これどうやってするの?Sugar ORMデータベースに画像を保存するにはどうしたらいいですか?

私はそれを保存しようとする次のコードを持っている:

エンティティ:

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; 
    } 
} 

をそして、私は次のコードで保存しようとしています:

Exercise addExercise = new Exercise("Prueba", "Prueba 2", image.getDrawable(),""); 
    addExercise.save(); 

可変画像ですImageView、保存する画像はギャラリーで撮影された画像です:

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)); 
     } 

私は次のエラーを表示され保存しよう:

Class cannot be read from Sqlite3 database. Please check the type of field image(android.graphics.drawable.Drawable)

ありがとうございました!

答えて

6

演習クラスで

protected byte[] image; 

定義し、また、コンストラクタでイメージ変数の種類を変更するには、この

のように試してみてください。 すぐバイト配列に画像を変換

ByteArrayOutputStream stream=new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.PNG,90,90,stream); 
    Byte[] image_byte=stream.toByteArray(); 

DBに画像全体が悪い考え読み出しに基づいて実際に格納/ efficiency.Itは別々のフォルダに画像を格納し、文字列としてパスを格納するのに優れている書き込み( varchar)データベース

関連する問題