user1002448
user1002448

Reputation: 415

how can I store image and retrieve the same in sqlite-android

I am developing an application that stores some static images in sqlite and I want to retrieve the same in a imageview. how do I do this thanks.

Upvotes: 1

Views: 2641

Answers (1)

idiottiger
idiottiger

Reputation: 5177

sqlite3 supports the blob type, you can save the bitmap content using the blob type.

However, the blob type has a size limit, and to save to a blob type is difficult.

So, I suggest to save the bitmap local or on the sdcard, and save the path in the database.

added :

table define a column with name 'image' using blob type

    Bitmap map = ...;
    ByteArrayOutputStream bufferStream = new ByteArrayOutputStream(16*1024);
    map.compress(CompressFormat.JPEG, 80, bufferStream);
    byte[] bytes = bufferStream.toByteArray();
    ContentValues values = new ContentValues();
    values.put("image", bytes);

convert the image byte array, using SQLiteDatabase class method or content provider as you like:

public long insert (String table, String nullColumnHack, ContentValues values)

to insert to table, so it save the image to blob.

and then: when you query the blob data, you can create the image like this:

        BitmapFactory.Options option2 = new BitmapFactory.Options();
        option2.inPreferredConfig = Bitmap.Config.RGB_565;
        // added for reducing the memory
        option2.inDither = false;
        option2.inPurgeable = true;
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, option2);

hope it can implement your request. -):

Upvotes: 7

Related Questions