加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在SQLite android中存储图像

发布时间:2020-12-12 18:53:55 所属栏目:百科 来源:网络整理
导读:我有一个SQLite数据库,我使用此代码将图像存储为BLOB URL url = new URL("http://t0.gstatic.com/images?q=tbn:ANd9GcRsaLl3TGB4W2hJFN_Wh0DNVPQEYGtweNsqvTXVtwE8FXR300-Ut-npgS4"); //open the connection URLConnection ucon = url.openConnection(); //b
我有一个SQLite数据库,我使用此代码将图像存储为BLOB

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();

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

工厂返回null

我的选择查询就是这个

mydb = openOrCreateDatabase(DBNAME,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,bb.length));
        }

请任何人帮忙.

解决方法

我使用decodeByteArray方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读