I have an app it can get image from my phone gallery but how I can store that image into SQLite database and get that image for user profile from the SQLite database.
-
1why you need to have image in db? that's bad idea for many reasons, discussed thousand of times. Google it and do not go that way – Marcin Orlowski Apr 12 '19 at 19:59
3 Answers
In order to store an image in the SQLite database you have to use "blob".
Examples:
Storing an image
public void insertImg(int id , Bitmap img ) {
byte[] data = getBitmapAsByteArray(img); // this is a function
insertStatement_logo.bindLong(1, id);
insertStatement_logo.bindBlob(2, data);
insertStatement_logo.executeInsert();
insertStatement_logo.clearBindings() ;
}
public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, outputStream);
return outputStream.toByteArray();
}
Retrieving an image
public Bitmap getImage(int i){
String qu = "select img from table where feedid=" + i ;
Cursor cur = db.rawQuery(qu, null);
if (cur.moveToFirst()){
byte[] imgByte = cur.getBlob(0);
cur.close();
return BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
}
if (cur != null && !cur.isClosed()) {
cur.close();
}
return null;
}
I suggest you to follow this tutorial

- 63
- 1
- 7
You can store the image as a blob
create your table this way
"CREATE TABLE TABLE_NAME_HERE (lastname TEXT, gender TEXT, signature BLOB)"
declare your person class as
public String Lastname;
public String Gender;
public byte[] Signature;
get the image as and convert to byte array
byte[] signatureByte = //get the image byte array here
add to db as
SQLiteDatabase db = OpenDb();
ContentValues values = new ContentValues();
values.put(KEY_LAST_NAME, person.Lastname);
values.put(KEY_GENDER, person.Gender);
values.put(KEY_SIGNATURE, person.Signature);
db.insert(TABLE_NAME_HERE, null, values);
retrieve as
SQLiteDatabase db = OpenDb();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME_HERE, null);
if (cursor.moveToFirst()) {
Person person = new Person();
person.Lastname = cursor.getString(cursor.getColumnIndex(KEY_LAST_NAME));
person.Gender = cursor.getString(cursor.getColumnIndex(KEY_GENDER));
person.Signature = cursor.getBlob(cursor.getColumnIndex(KEY_SIGNATURE));
return person;
}
display as
signature.setImageBitmap(BitmapFactory.decodeByteArray(person.Signature, 0, person.Signature.length))
Alternatively, you can store the image in your application folder and store a reference to it in you db. that way you can retrieve it.

- 1,536
- 2
- 13
- 25
Your first issue you should consider is whether or not you should be saving images in the database.
Generally storing images it is not a good idea.
For Android storing and retrieving large images say over 100KB can be problematic. Under such a size (perhaps larger) SQLite can actually be quite efficient 35% Faster Than The Filesystem(Note that this document is relatively old)).
Unless you devise alternatives to the standard Android SDK then the absolute maximum size of an image that can be retrieved is under 2MB. That is due to a CursorWindow (a buffer for the returned rows) being only 2MB.
In short if any image to be stored is nearing 2MB or is 2MB or larger then you will not be able to retrieve it without complicating matters. Such a method is explained in this Q and A, How to use images in Android SQLite that are larger than the limitations of a CursorWindow? Note this method is not recommended.
What is recommended is that images are saved to disk and that the path (or enough of the path to uniquely identify the full path to the image) is instead stored. Thus you retrieve that path from the database and then retrieve the image from the extracted path.
The Answer at How can I insert image in a sqlite database. Demonstrates both this methodology and also of saving smaller images (100k or less) in the database (taking advantage of 35% Faster Than The Filesystem).

- 51,415
- 16
- 49
- 68