我如何 return 来自 SQLite 数据库的位图

How can I return a bitmap from SQLite database

我的服务器上有图像我想下载这些图像并将它们存储在本地数据库中(使用 SQLite)然后我想在本地使用这些图像。

所以我将图像作为位图存储到我的 SQLite 数据库中,我需要从该数据库中以位图而不是字节的形式检索它。

这是我的代码..

从 url 获取图像:

private Bitmap GetImageBitmapFromUrl1(string url)
{
    Bitmap imagebitmap = null;
    using (var webClient = new WebClient())
    {
        var imageBytes = webClient.DownloadData("..." + url);
        if (imageBytes != null && imageBytes.Length > 0)
        {
            imagebitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
        }
    }
    return imagebitmap;
}

使用 SQLite 创建本地数据库:

// SQLite.. Creating database to store images 
private class MainDatabase
{
    // Constructor
    public MainDatabase()
    {
        if(!System.IO.File.Exists(folder))
        {
            var db = new SQLiteConnection(folder);
            db.CreateTable<Image>();
        }
    }

    // Create database
    string folder = System.IO.Path.Combine
            (System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),
            "imagesDatabase.db");

    // Create Table
    [Table("Image")]
    public class Image
    {
        [Column("_id"), PrimaryKey , AutoIncrement]
        public int _id { set; get; }

        [Column("_image")]
        public Bitmap _image { set; get; }
        }

        //Insert data
        public void Insert(Bitmap image)
        {
            var db = new SQLiteConnection(folder);
            var imageColumn = new Image();
            imageColumn._image = image;
            db.Insert(imageColumn);

        }

        //Retrieving data

        public Bitmap GetImageById(int id)
        {
            var db = new SQLiteConnection(folder);

            var image = from p in db.Table<Image>()
                        where p._id == id
                        select p._image;

            return image; // doesn't work   
        }
    }

我想 return 它的 ID。

So I've stored an image as bitmap to my SQLite Database, and I need to retrieve it from that database as bitmap too not as bytes.

位图不能直接存储在SQLite中。当 运行 你的代码时,我得到了不支持的类型错误。在 SQLite 中存储 Bitmap 的唯一方法是在字节数组中存储和检索它。但是您可以将转换过程包装在您的方法中,并直接在您的 Activity:

中传递 Bitmap
public class MainDatabase
{
    // Constructor
    public MainDatabase()
    {
        //if (!System.IO.File.Exists(folder))
        //{
            var db = new SQLiteConnection(folder);
            db.CreateTable<Image>();
        //}
    }

    // Create database
    string folder = System.IO.Path.Combine
            (System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),
            "imagesDatabase.db");

    // Create Table
    [Table("Image")]
    public class Image
    {
        [Column("_id"), PrimaryKey, AutoIncrement]
        public int _id { set; get; }

        [Column("_image")]
        public byte[] _image { set; get; }// store the image with byte[]
    }

    public byte[] GetBitmapAsByteArray(Bitmap image)
    {
        MemoryStream stream = new MemoryStream();
        image.Compress(Bitmap.CompressFormat.Png, 0, stream);
        return stream.ToArray();
    }

    //Insert data
    public int Insert(Bitmap image)
    {
        var db = new SQLiteConnection(folder);
        var imageColumn = new Image();
        imageColumn._image = GetBitmapAsByteArray(image);
        return db.Insert(imageColumn);
    }

    //Retrieving data

    public Bitmap GetImageById(int id)
    {
        var db = new SQLiteConnection(folder);
        Image image=db.Find<Image>(id);
        // return the decoded Bitmap
        return BitmapFactory.DecodeByteArray(image._image, 0, image._image.Length);
    }
}