15

I'd like to know if there's a way to save Images (of the type .gif) to the sqllite-database. If yes how should my DatabaseAdapter look like.

Also is there a performance issue?

Macarse
  • 89,619
  • 44
  • 170
  • 229
safari
  • 7,257
  • 17
  • 52
  • 81

3 Answers3

20

You should use BLOB in your database:

Check this tutorial...

But I think you should download and store image in HashMap, which will make it simpler.

Code:

Stroring

var imageMap = new HashMap<String, byte[]>();

var imageUrl = "http://i.stack.imgur.com/TLjuP.jpg";

var imagedata = GetImage(imageUrl);      

imageMap.put("img",imagedata);


Retrieving

var imageData = imageMap.get("img");

var imageStream = new ByteArrayInputStream(imageData);

var image = BitmapFactory.decodeStream(imageStream);


GetImage

private byte[] GetImage(String url)
{
    try
    {
        var imageUrl = new URL(url);
        var urlConnection = imageUrl.openConnection();

        var inputStream = urlConnection.getInputStream();
        var bufferedInputStream = new BufferedInputStream(inputStream);

        var byteArrayBuffer = new ByteArrayBuffer(500);

        int current = 0;
        while ((current = bis.read()) != -1)
        {
            byteArrayBuffer.append((byte)current);
        }

        return byteArrayBuffer.toByteArray();
    }
    catch (Exception e)
    {
        Log.d("ImageManager", "Error: " + e.toString());
        return null;
    }       
}

Hope it helps you.

LoukMouk
  • 494
  • 8
  • 23
Siten
  • 4,463
  • 9
  • 37
  • 63
  • well that is kind of data type, no not much effect on DB. http://www.akadia.com/services/dotnet_load_blob.html – Siten Sep 22 '11 at 09:08
  • Well maybe it does not have any effect on DB, but how about overall performance? I doubt that DB is faster then normal file loading – Bojan Kogoj Sep 22 '11 at 09:11
  • 1
    @BojanKogoj DB will be faster if one's going to use bunch of images (hundreds or thousands) - in this case DB is preferable comparing to FS – Barmaley Sep 22 '11 at 12:39
  • 2
    Why not use `BitmapFactory.decodeByteArray(hh.get("img"), 0, 0)` to decode the image instead of creating an additional stream? – Moog Sep 22 '11 at 16:53
6

There's nothing special in storing image to SQLite. Just create table with BLOB record type and do smth like:

protected long saveBitmap(SQLiteDatabase database, Bitmap bmp)
{
    int size = bmp.getRowBytes() * bmp.getHeight(); 
    ByteBuffer b = ByteBuffer.allocate(size); bmp.copyPixelsToBuffer(b); 
    byte[] bytes = new byte[size];
    b.get(bytes, 0, bytes.length);
    ContentValues cv=new ContentValues();
    cv.put(CHUNK, bytes);
    this.id= database.insert(TABLE, null, cv);
}

Probably you migth want to save image chunk by chunk, since there's limits/recommended BLOB size (don't really recall how much)

Barmaley
  • 16,366
  • 18
  • 70
  • 144
  • can I only insert bitmap pictures or does it also work with gif's png's and so on? – safari Sep 22 '11 at 12:26
  • It doesn't matter which format of image u're going to insert. Any image can be represented as Android Bitmap - which is not actually Windows BMP (name is confusing), but just internal representation of PNG/GIF/JPEG/and so on in Android – Barmaley Sep 22 '11 at 12:29
2

Check this tutorial, it should show you what you need.
Another useful link.

Marco
  • 55,302
  • 13
  • 128
  • 150