繁体   English   中英

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

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

我正在开发一个脱机应用程序,但没有从用户的画廊获得任何图片。 但是我想向手动插入的用户显示图像。

我能想到的唯一解决方案是将这些图像放入drawable ,然后将其保存到sqlite数据库(我不确定如何)。 能做到吗?

谢谢。

如果您愿意将图像存储在可绘制的文件夹中,则可以在数据库中存储对其的引用。 只需存储com.example.yourapp:drawable/imagefilename类的字符串,然后使用它来显示图像,其中yourString包含数据库中的字符串。

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

您可以为此使用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