In my project I need to store an image into a sqlite database and also need to retrieve it to show in my android emulator. When I show it directly after decoding the encoded string, which I got from Java class using sockets, the image displays there. But when I store a byte array code of the string into the sqlite database with the datatype blob and then again retrieve it by using the getblob()
function it contains a different value and this error occurs:
JAVA.lang.NULLPointerException: Factory returns null.
I need a suggestion to store a bitmap image into a sqlite database and also to retrieve it from the sqlite database.
Inorder to store images to android SQLite database, you need to convert the image uri to bitmap then to binary characters that is, bytes[] sequence. Then set the table column data type as BLOB data type. After retrieving the images from DB, convert the byte[] data type to bitmap in order to set it to imageview.
We can retrieve anything from database using an object of the Cursor class. We will call a method of this class called rawQuery and it will return a resultset with the cursor pointing to the table. We can move the cursor forward and retrieve the data. This method return the total number of columns of the table.
If you have Bitmap image then you can do following. Bitmap photo = <Your image> ByteArrayOutputStream bos = new ByteArrayOutputStream(); photo. compress(Bitmap. CompressFormat.
Setting Up the database
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); } }
Insert in the Database:
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 ); }
Retrieving data:
byte[] image = cursor.getBlob(1);
Note:
Below is an Utility class which I hope could help you:
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); } }
Further reading
If you are not familiar how to insert and retrieve into a database, go through this tutorial.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With