Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Setting Up the database</strong><br></p> <pre><code>public class DatabaseHelper extends SQLiteOpenHelper { // Database Version private static final int DATABASE_VERSION = 1; // Database Name private static final String DATABASE_NAME = "database_name"; // Table Names private static final String DB_TABLE = "table_image"; // column names private static final String KEY_NAME = "image_name"; private static final String KEY_IMAGE = "image_data"; // Table create statement private static final String CREATE_TABLE_IMAGE = "CREATE TABLE " + DB_TABLE + "("+ KEY_NAME + " TEXT," + KEY_IMAGE + " BLOB);"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { // creating table db.execSQL(CREATE_TABLE_IMAGE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // on upgrade drop older tables db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE); // create new table onCreate(db); } } </code></pre> <p><strong>Insert in the Database:</strong><br></p> <pre><code>public void addEntry( String name, byte[] image) throws SQLiteException{ SQLiteDatabase database = this.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put(KEY_NAME, name); cv.put(KEY_IMAGE, image); database.insert( DB_TABLE, null, cv ); } </code></pre> <p><strong>Retrieving data</strong>:<br></p> <pre><code> byte[] image = cursor.getBlob(1); </code></pre> <p><strong>Note:</strong><br></p> <ol> <li>Before inserting into database, you need to convert your Bitmap image into byte array first then apply it using database query.</li> <li>When retrieving from database, you certainly have a byte array of image, what you need to do is to convert byte array back to original image. So, you have to make use of BitmapFactory to decode.</li> </ol> <p>Below is an Utility class which I hope could help you:</p> <pre><code>public class DbBitmapUtility { // convert from bitmap to byte array public static byte[] getBytes(Bitmap bitmap) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 0, stream); return stream.toByteArray(); } // convert from byte array to bitmap public static Bitmap getImage(byte[] image) { return BitmapFactory.decodeByteArray(image, 0, image.length); } } </code></pre> <p><br><strong>Further reading</strong> <br> If you are not familiar how to insert and retrieve into a database, go through this <a href="http://mrbool.com/how-to-insert-data-into-a-sqlite-database-in-android/28895" rel="noreferrer">tutorial</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload