在SQLite安卓中存储图像

4

我有一个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, 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));
        }

这是我的logcat输出 请有经验的人帮忙解决。


什么是错误?请包含您的日志输出:通过更快、更简单的方式找出您做错了什么。 - Femi
1
这个问题已经在几篇文章中讨论过了,建议不要将图像保存在数据库中,而是存储图像SD卡路径:http://stackoverflow.com/questions/3748727/sqlite-in-android http://stackoverflow.com/questions/3433991/displaying-images-from-sqlite-in-android - pandre
1个回答

2
我使用 decodeByteArray 方法
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);
}

我也在使用同样的东西。 - James

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接