Skip to content

Instantly share code, notes, and snippets.

@yudikarma
Created January 2, 2019 09:14
Show Gist options
  • Save yudikarma/27e559db94d67ea3be2d5cac147e069e to your computer and use it in GitHub Desktop.
Save yudikarma/27e559db94d67ea3be2d5cac147e069e to your computer and use it in GitHub Desktop.
Sqlite Helper for add record with image blop
package com.example.yudikarma.androidcamera.Sqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.util.Log;
import com.example.yudikarma.androidcamera.Model.ImageModel;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
public class MyDBHandler extends SQLiteOpenHelper {
//information of database
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "SqliteImage.db";
public static final String TABLE_NAME = "ImagesInformation";
public static final String COLUMN_ID = "id";
public static final String COLUMN_WORK_ID = "work_id";
public static final String COLUMN_TAKEN = "taken";
public static final String COLUMN_LATITUDE = "latitude";
public static final String COLUMN_LONGITUDE = "longitude";
public static final String COLUMN_IMAGE = "image";
//initialize the database
public MyDBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY,"
+ COLUMN_WORK_ID + " INTEGER,"
+ COLUMN_TAKEN + " TEXT,"
+ COLUMN_LATITUDE + " FLOAT,"
+ COLUMN_LONGITUDE + " FLOAT,"
+ COLUMN_IMAGE + " BLOB" + ")";
sqLiteDatabase.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
public void addRecord(ImageModel imageModel) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_WORK_ID, imageModel.getWork_id());
values.put(COLUMN_TAKEN, imageModel.getTaken());
values.put(COLUMN_LATITUDE, imageModel.getLatitude());
values.put(COLUMN_LONGITUDE, imageModel.getLongitude());
values.put(COLUMN_IMAGE, imageModel.getImage());
db.insert(TABLE_NAME, null, values);
db.close();
}
// Getting single image
public int numberOfRows() {
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, TABLE_NAME);
return numRows;
}
public Bitmap getImage() {
String qu = "SELECT * FROM " + TABLE_NAME;
Cursor cur = null;
SQLiteDatabase db = this.getReadableDatabase();
try {
cur = db.rawQuery(qu, new String[]{});
} catch (Exception e) {
/*AppLog.exception(e);*/
}
if (cur != null) {
if (cur.moveToLast()) {
int index = cur.getColumnIndexOrThrow("image");
byte[] imgByte = cur.getBlob(index);
cur.close();
return BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
}
if (cur != null && !cur.isClosed()) {
cur.close();
}
}
return null;
}
public ArrayList<ImageModel> getAllRecord() {
ArrayList<ImageModel> listImagemodel = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_NAME+" ORDER BY "+COLUMN_ID+" DESC";
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
Cursor cursor = null;
try {
cursor = sqLiteDatabase.rawQuery(selectQuery, null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
int indexID = cursor.getColumnIndexOrThrow("id");
int indexWorkId = cursor.getColumnIndexOrThrow("work_id");
int indexTaken = cursor.getColumnIndexOrThrow("taken");
int indexLatitude = cursor.getColumnIndexOrThrow("latitude");
int indexLongitude = cursor.getColumnIndexOrThrow("longitude");
int indeximage = cursor.getColumnIndexOrThrow("image");
do {
ImageModel imageModel = new ImageModel();
imageModel.setId(cursor.getInt(indexID));
imageModel.setWork_id(cursor.getInt(indexWorkId));
imageModel.setTaken(cursor.getString(indexTaken));
imageModel.setLatitude(cursor.getFloat(indexLatitude));
imageModel.setLongitude(cursor.getFloat(indexLongitude));
/*byte[] imagebyte = cursor.getBlob(indeximage);*/
imageModel.setImage(cursor.getBlob(indeximage));
listImagemodel.add(imageModel);
} while (cursor.moveToNext());
}
}finally {
try { cursor.close(); } catch (Exception ignore) {}
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
} finally {
try { sqLiteDatabase.close(); } catch (Exception ignore) {}
}
Log.e("Image database Sqlite",""+listImagemodel.size());
return listImagemodel;
}
public byte[] getBitmapAsByteArray(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 0, outputStream);
return outputStream.toByteArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment