简体   繁体   English

Android-将图像存储到SQLite的最佳方法是什么?

[英]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). 我能想到的唯一解决方案是将这些图像放入drawable ,然后将其保存到sqlite数据库(我不确定如何)。 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.. 只需存储com.example.yourapp:drawable/imagefilename类的字符串,然后使用它来显示图像,其中yourString包含数据库中的字符串。

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

You can Use insert blob for this 您可以为此使用insert blob

    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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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