How to insert image data to sqlite database in Android and how to retrieve it back
I ran across doing something that would required me to get an image from the internet and save it in the database instead of saving it in the file system then retrieving back the stored image from the database to be displayed in ImageView. Using other database server, we usually use blob to store binary data to the database, since SQLite has no field type we could use BLOB as its field type. Your create statement should appear like this
To get the image from the internet and store it on our database
Explanation
Create a new HTTP Client
DefaultHttpClient mHttpClient = new DefaultHttpClient();
Make a request to get our image
HttpGet mHttpGet = new HttpGet("your image url");
HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
See if our request is a 200 or has an okay return status
if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
Get the entity of our request
HttpEntity entity = mHttpResponse.getEntity();
Convert the content of the entity to byte array so that we could store it on our database
values.put(MyBaseColumn.MyTable.ImageField, EntityUtils.toByteArray(entity));
Insert our image to the databse
getContentResolver().insert(MyBaseColumn.MyTable.CONTENT_URI, values);
Retrieving the image back
Here is it assumed that you are using Custom CursorAdaptor, see Custom View in your ListActivity and Custom Adapter in Android
Explanation
Get the image back from our database
byte[] bb = cursor.getBlob(cursor.getColumnIndex(MyBaseColumn.MyTable.ImageField));
Decode our image to become a bitmap so that our imageView will display it properly
myImage.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));
CREATE TABLE storedImages (_id INTEGER PRIMARY KEY, myImage BLOB)
To get the image from the internet and store it on our database
DefaultHttpClient mHttpClient = new DefaultHttpClient();
HttpGet mHttpGet = new HttpGet("your image url");
HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = mHttpResponse.getEntity();
if ( entity != null) {
// insert to database
ContentValues values = new ContentValues();
values.put(MyBaseColumn.MyTable.ImageField, EntityUtils.toByteArray(entity));
getContentResolver().insert(MyBaseColumn.MyTable.CONTENT_URI, values);
}
}
Explanation
Create a new HTTP Client
DefaultHttpClient mHttpClient = new DefaultHttpClient();
Make a request to get our image
HttpGet mHttpGet = new HttpGet("your image url");
HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
See if our request is a 200 or has an okay return status
if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
Get the entity of our request
HttpEntity entity = mHttpResponse.getEntity();
Convert the content of the entity to byte array so that we could store it on our database
values.put(MyBaseColumn.MyTable.ImageField, EntityUtils.toByteArray(entity));
Insert our image to the databse
getContentResolver().insert(MyBaseColumn.MyTable.CONTENT_URI, values);
Retrieving the image back
Here is it assumed that you are using Custom CursorAdaptor, see Custom View in your ListActivity and Custom Adapter in Android
ImageView myImage = (ImageView) findViewById(R.id.myImage);
byte[] bb = cursor.getBlob(cursor.getColumnIndex(MyBaseColumn.MyTable.ImageField));
myImage.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));
Explanation
Get the image back from our database
byte[] bb = cursor.getBlob(cursor.getColumnIndex(MyBaseColumn.MyTable.ImageField));
Decode our image to become a bitmap so that our imageView will display it properly
myImage.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));
No comments: