简体   繁体   中英

Adding an Image to a sqlite database

I've been struggling for a while now with this, and i want to be able to store an image to my database. I know storing an image to a database directly is bad practice but i simply do not know enough to be able to do it another way.

So, i'm currently stuck with a few issues.

Firstly, I'm not even sure my path is correct; i want to get a drawable file and store it to my database and there must be an easier way than doing a path straight from the C drive right?

Secondly, I don't know much about this but i need to convert my file to a bitmap so that it can be converted to a byte array? And i'm not sure how to do this exactly.

I've tried several things, wrote this code out about 10 times already in different ways and not getting anywhere. Thanks all for help in advance.

   public void insertAvatar(String Email, byte[] head) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    String sql = "INSERT INTO Avatar VALUES (?, ?)";

    File head = new File("C:\\Users\\PC\\Desktop\\capaa\\src\\main\\res\\drawable\\head2.png");
    Bitmap imageToStoreBitmap = head; // doesn't work as my file isnt a bitmap yet
    objectByteArrayOutputStream = new ByteArrayOutputStream();

    imageToStoreBitmap.compress(Bitmap.CompressFormat.JPEG, 100, objectByteArrayOutputStream);
    imageInBytes = objectByteArrayOutputStream.toByteArray();
    contentValues.put("Email", Email);
    contentValues.put("head", imageInBytes);
    long checkIfQueryRuns = db.insert("Avatar", null, contentValues );
}

You need to use Blob to store images in your SQLite database.

Create a table to store the images

CREATE TABLE " + DB_TABLE_NAME + "("+ 
                   KEY_NAME + " TEXT," + 
                   KEY_IMAGE + " BLOB);";

To store an image in the table

public void addImage( String name, byte[] image) throws SQLiteException{
    ContentValues cv = new  ContentValues();
    cv.put(KEY_NAME,    name);
    cv.put(KEY_IMAGE,   image);
    database.insert( DB_TABLE_NAME, null, cv );
}

As you can see before inserting the image to the table, you need to convert the bitmap to a byte array.

// To convert from bitmap to byte array
public static byte[] getImageBytes(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, stream);
    return stream.toByteArray();
}

To retrieve image from database

//search your image using the key and get the cursor
............
byte[] image = cursor.getBlob(1);
............

As you can see the image returned is in a byte array. Now you can convert this byte array to bitmap to use in your app.

//To convert from byte array to bitmap
public static Bitmap getImage(byte[] image) {
    return BitmapFactory.decodeByteArray(image, 0, image.length);
}

Having written the answer, I myself is not a big fan of storing images in the database. I am not sure what is your need for storing the images but you can check the following libraries to handle images in your app.

https://github.com/bumptech/glide/

https://square.github.io/picasso/

https://github.com/nostra13/Android-Universal-Image-Loader

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM