How to get image from SQLite database?

How to get image saved in Sqlite table?

the table is as follows:

public class ImageInDB extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 2; private static final String DATABASE_NAME = "test01.db"; private static final String MP_TABLE_NAME = "MPImage"; ImageInDB (Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); }// end of @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL(" CREATE TABLE " + MP_TABLE_NAME + " ( " + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + " img BLOB " + ");" ); } 

Now, how should a method that extracts an image llok how ?? below is my attempt:

 public ??? getMP_RPY(long id) { SQLiteDatabase db = this.getReadableDatabase(); SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT img FROM MPImage WHERE "+ BaseColumns._ID+" = "+ Long.toString(id), null); c.moveToFirst(); ???? r = c.getDouble(0); return r; } 
+4
source share
2 answers
 ImageView myImage = (ImageView) findViewById(R.id.myImage); byte[] bb = cursor.getBlob(cursor.getColumnIndex(MyBaseColumn.MyTable.ImageField)); myImage.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length)); 

OR

Please check this link.

+5
source

Do not store the image in a SQLite database. Instead, save the path to the image. Or, if you REALLY want to save an image to a database, check out the basic SQ Lite.

+6
source

Source: https://habr.com/ru/post/1397748/


All Articles