开发者

Storing images in SQLite android

开发者 https://www.devze.com 2023-03-24 11:44 出处:网络
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\"

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

Factory returned null

My Select Query is this

 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 = (Image开发者_StackOverflow社区View) 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));
        }

Storing images in SQLite android

Please anyone help.


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消