如何从SQLite数据库检索图像? [英] How to retrieve an image from a SQLite database?

查看:259
本文介绍了如何从SQLite数据库检索图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将从相机拍摄的图像存储到sqlite数据库中,有人可以帮助我检索相同的图像,并且我想在图像视图中显示该图像.

I am storing an image taken from camera into sqlite database can anybody help me to retrieve the same image and i want to show that image in a image view.

这是我的数据库处理程序类用于保存图像的方法

Here is my Database handler class method for saving the image

public long insert(Bitmap img ) {

        SQLiteDatabase base=this.getWritableDatabase();
        byte[] data = getBitmapAsByteArray(img); // this is a function
        ContentValues value=new ContentValues();
        value.put("image",data);

        long a= base.insert("Mypic", null, value); 
        System.out.println("check1" + a);

        return a; 
    }


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

请帮助我编写如何获取图像并在imageview中显示图像的方法.

Please help me for writing the method how i can get the image and display the image in an imageview.

推荐答案

要在数据库上插入图片,

To insert Image on database:

    Bitmap bitmap = ((BitmapDrawable) image_imgv.getDrawable()).getBitmap();

    ByteArrayOutputStream bos4 = new ByteArrayOutputStream();
    bitmap4.compress(Bitmap.CompressFormat.PNG, 100, bos4);
    image = bos4.toByteArray();

    database = new BBDD(this, "BBDD", null, 1);
    SQLiteDatabase db = database.getWritableDatabase();

    ContentValues reg = new ContentValues();
    reg.put("img", image);

要检索:

database2 = new BBDD(Activity.this, "BBDD", null, 1);
            SQLiteDatabase db2 = database2.getReadableDatabase();

            if (db2 != null)
            {
                Cursor cursor = db2.rawQuery("SELECT img FROM database2, null);
                if (cursor.moveToFirst())
                {
                    img=cursor.getBlob(cursor.getColumnIndex("img"));
                    Bitmap b1=BitmapFactory.decodeByteArray(image, 0, image.length);
                    image_imageview.setImageBitmap(b1);

                }
                else
                    Toast.makeText(Activity.this, "Error.", Toast.LENGTH_LONG).show();

                db2.close();
            }
            else
                Toast.makeText(sActivity.this, "Error db.", Toast.LENGTH_LONG).show();
        }
    });

这篇关于如何从SQLite数据库检索图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆