Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save images into Database

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?

like image 483
safari Avatar asked Sep 22 '11 08:09

safari


People also ask

Can you save images to a database?

A database gives you the opportunity to store photos and other small images in a database table. You can create such a database table for example when you want to create an online photo album with descriptions of your photos. Storing images in a database table is not recommended.

Can we save images in SQL database?

The IMAGE data type in SQL Server has been used to store the image files. Recently, Microsoft began suggesting using VARBINARY(MAX) instead of IMAGE for storing a large amount of data in a single column since IMAGE will be retired in a future version of MS SQL Server.

How do I store images in SQL database?

Insert one image into SQL Server This table will have an integer (int) id and the image column named img. The data type that we are going to use to store images is the varbinary(max). The INSERT statement inserts the value 1 as the id and then inserts the image named 1. png from the folder img in the c drive.

Which database is best for storing images?

Store in Couchbase a metadata JSON document for each object, maybe a small thumbnail image at most. In that document is data you need about that object in your application quickly, but also a pointer to a purpose built object store like S3, a file system or HDFS. You will get the best of all worlds.


2 Answers

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.

like image 65
Siten Avatar answered Oct 12 '22 17:10

Siten


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)

like image 33
Barmaley Avatar answered Oct 12 '22 17:10

Barmaley