SQLite中的android存储图像 [英] Storing images in SQLite android

查看:96
本文介绍了SQLite中的android存储图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQLite数据库中,我存储使用这个code图像作为BLOB

I have an SQLite Database in which I am storing images as BLOB using this code

URL url = new URL("http://t0.gstatic.com/images?q=tbn:ANd9GcRsaLl3TGB4W2hJFN_Wh0DNVPQEYGtweNsqvTXVtwE8FXR300-Ut-npgS4");
        //open the connection
        URLConnection ucon = url.openConnection();
        //buffer the download
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is,128);
        ByteArrayBuffer baf = new ByteArrayBuffer(128);
        //get the bytes one by one
        int current = 0;
        while ((current = bis.read()) != -1) {
                baf.append((byte) current);
        }

     mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
     mydb.execSQL("INSERT INTO " + TABLE + "(IMAGE) VALUES('" + baf.toByteArray() + "')");
     mydb.close();

当我尝试以检索图像,我收到以下错误

When I try to retrive the image, I am getting the following error

厂返回null

我的选择查询是这样

 mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        Cursor allrows  = mydb.rawQuery("SELECT * FROM "+  TABLE, null); 
        if(allrows.getCount() > 0){
            allrows.moveToNext();
            System.out.println("3333");
            ImageView myImage = (ImageView) findViewById(R.id.ImageView01);
            byte[] bb = allrows.getBlob(1);
            System.out.println("VVV " + bb);
            //convert it back to an image
            ByteArrayInputStream imageStream = new ByteArrayInputStream(bb);
            Bitmap theImage = BitmapFactory.decodeStream(imageStream);

            myImage.setImageBitmap(theImage);
            //myImage.setBackgroundResource(R.drawable.icon);
            System.out.println("3333");
            //myImage.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));
        }

请人帮忙。

Please anyone help.

推荐答案

我用德codeByteArray方法

I use decodeByteArray method

byte[] userPic1Blob = cursor.getBlob(cursor.getColumnIndex(SqlConstans.USER_PIC1_BLOB));
if(userPic1Blob != null && userPic1Blob.length > 0){
    Bitmap bm = BitmapFactory.decodeByteArray(userPic1Blob, 0, userPic1Blob.length);
    imageView.setImageBitmap(bm);
}

这篇关于SQLite中的android存储图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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