简体   繁体   中英

Android - What's the best way to store image to SQLite?

I am developing an offline application and I'm not getting any picture from user's gallery. But I want to display the images to the users that I inserted manually.

The only solution I can think of is to put those images into drawable , then save it to the sqlite database (which I not sure how). Can this be done?

Thank you.

If you're willing to store the images in the drawable folders, you can store a reference to them in your database. Just store a string like com.example.yourapp:drawable/imagefilename and use this to display the image, where yourString contains the string from the database..

int imageResource = this.getResources().getIdentifier(yourString, null, null);
yourimageview.setImageResource(imageResource);

You can Use insert blob for this

    public void insertImg(int id , Bitmap img ) {   


    byte[] data = getBitmapAsByteArray(img); // this is a function

    insertStatement_logo.bindLong(1, id);       
    insertStatement_logo.bindBlob(2, data);

    insertStatement_logo.executeInsert();
    insertStatement_logo.clearBindings() ;

}

 public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, outputStream);       
    return outputStream.toByteArray();
}

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