如何在sqlite-android中存储图像并检索相同的图像

use*_*448 1 sqlite android image

我正在开发一个应用程序,它在sqlite中存储一些静态图像,我想在imageview中检索它.我该怎么做呢谢谢.

idi*_*ger 7

sqlite3支持该blob类型,可以使用该blob类型保存位图内容.

但是,blob类型具有大小限制,并且难以保存为blob类型.

因此,我建议将位图保存在本地或SD卡上,并将路径保存在数据库中.

补充说:

table使用blobtype 定义名为'image'的列

    Bitmap map = ...;
    ByteArrayOutputStream bufferStream = new ByteArrayOutputStream(16*1024);
    map.compress(CompressFormat.JPEG, 80, bufferStream);
    byte[] bytes = bufferStream.toByteArray();
    ContentValues values = new ContentValues();
    values.put("image", bytes);
Run Code Online (Sandbox Code Playgroud)

使用SQLiteDatabase类方法或内容提供程序转换图像字节数组:

public long insert (String table, String nullColumnHack, ContentValues values)

要插入到表中,所以它将图像保存到blob.

然后:当您查询blob数据时,您可以像这样创建图像:

        BitmapFactory.Options option2 = new BitmapFactory.Options();
        option2.inPreferredConfig = Bitmap.Config.RGB_565;
        // added for reducing the memory
        option2.inDither = false;
        option2.inPurgeable = true;
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, option2);
Run Code Online (Sandbox Code Playgroud)

希望它能够实现您的要求. - ):