If you want to store large images (full-screen), it's advisable to
store only the file path in the database, having said that, let's
answer your question:
1. Define the column that holds the image as BLOB.
2. To make sure you are not storing duplicate images, store the hash-
code of the image along with the image (I'll get to that later)
3. So, it's a good idea to create a table that holds only the images,
and use reference to this table from other tables.
4. To create the table you can use code similar to the following:
db.execSQL("CREATE TABLE images ("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "data BLOB,"
+ "hash BLOB UNIQUE"
+ ");");
5. May I suggest that you store your table name and column names in
predefined constants, and use a StringBuilder to build SQL queries,
instead of concatenating strings to prevent unnecessary garbage
collection :-)
6. To convert the image to a BLOB, use the following code: (Note that
we're compressing the image here)
private byte[] getBitmapAsByteArray(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// Middle parameter is quality, but since PNG is lossless, it
doesn't matter
bitmap.compress(CompressFormat.PNG, 0, outputStream);
return outputStream.toByteArray();
}
7. To read this data from the database, you will do something like:
byte[] bitmapData = cursor.getBlob(cursor.getColumnIndex("data"));
8. To convert this data back to an image, use:
BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length);
9. Basically that already answers your question, but I promised you
the image hash-code, so here goes:
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(bitmapData); // It's the same bitmap data that you got from
getBitmapAsByteArray
byte[] digest = md.digest();
10. Like you could see from the table creation, this is also stored as
a BLOB.
11. If you want to query the hash code (Probably to see if an image
with the hash hash, already exists(, you cannot use the blob in a
SELECT statement, you must convert the hash blob to a blob-string (in
the form of x'abcde...', with the following code:
private static char toHexDigit(int b) {
return (char)((b < 10) ? ('0' + b) : ('A' + b - 10));
}
public static String toBlobString(byte[] bytes) {
final int length = bytes.length;
char chars[] = new char[length * 2 + 3];
chars[0] = 'x';
chars[1] = '\'';
int index = 2;
for (int n = 0; n < length; n++) {
int value = bytes[n] & 0xff;
chars[index++] = toHexDigit(value / 16);
chars[index++] = toHexDigit(value % 16);
}
chars[index] = '\'';
return new String(chars);
}
Regards,
Lior - Developer of UltimateFaves / UltimateFavesPRO
http://ultimatefaves.wordpress.com/
https://mobentoo.com/application/ultimate-faves-pro
This is what I do and it works very well,
On Nov 4, 7:40 am, devi <[email protected]> wrote:
> hi!
>
> I want to store Images(using insert statement) in sqlite
> database & retrive that image from database and display that image on
> android emulator...can anyone give sample code?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en