2017-03-26 2 views
0

私はAndroidスタジオにはとても新しいので、これで問題があります。次のように私は、データベースにデータをハードコードしているSQLiteデータベースにBLOBとして保存された画像を表示するには

public void onCreate(SQLiteDatabase db){ 
     db.execSQL(create_table); 
    } 

private static final String create_table = "create table if not exists Test ("+ 
      "EntryID integer primary key autoincrement, "+ 
      "Description string,"+ 
      "Picture blob"+ 
      ")"; 

:私はアンドロイドのスタジオでのSQLiteデータベースに画像を保存している

ContentValues cv5 = new ContentValues(); 
    cv5.put("EntryID",1); 
    cv5.put("Description","The club was founded in 1885"; 
    cv5.put("Picture","club.png"); 
    sdb.insert("Test",null,cv5); 

を別のクラスでは、私はこの保存された画像を表示しようとしていますし、私はそれに多くの問題を抱えています。私は前にBLOBに出会ったことはありません。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Drawable myDrawable = getResources().getDrawable(R.drawable.club); 
    image.setImageDrawable(myDrawable); 
} 

このクラスでイメージを設定しようとすると、描画可能であるとエラーが発生します。私が言ったように私は

答えて

1

cv5.put( "ピクチャー"、 "club.png")をthis.``するのは非常に新しいです。それは

間違った方法だあなたは、BLOBに画像を変換する前に、あなたがイメージ

byte[] photo=cursor.getBlob(index of blob cloumn); 
ByteArrayInputStream imageStream = new ByteArrayInputStream(photo); 
Bitmap bitmap= BitmapFactory.decodeStream(imageStream); 
image.setImageBitmap(bitmap); 
+0

にBLOBをデコードする必要がある。その後その

ByteArrayOutputStream outStreamArray = new ByteArrayOutputStream(); Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.common)).getBitmap(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStrea); byte[] photo = outStreamArray.toByteArray();cv5.put("Picture", photo) 

のようなものを//必要なあなたにアレックスをありがとうございました。 Blobをどこでイメージにデコードしていますか?私はそれを追加することはできません "非静的メソッドgetBlob(int)は静的コンテキストから参照することはできません" – Sandra123

関連する問題