android - 在 SQLite android 中存储图像

标签 android

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

Here is my logcat output 请任何人帮助。

最佳答案

我使用 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);
}

关于android - 在 SQLite android 中存储图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6915775/

相关文章:

android - 如何解决套件构建期间的异常?

android - 浏览多个 Activity

android - 异步任务的 Gridview 回收问题

按下后退按钮后 Android ViewPager 异常

android - 更新 getCheckedItemPositions()

java - 自定义 TextView 字体不适用于设置来自 java android 的文本

java - 当用户关闭所需的应用程序时,辅助服务会删除图标

android - 使用 Activity 上下文观察 fragment 中的实时数据对象?

android - Grafika 和 OpenGL 在 android 上以方形录制视频

android - Layout_height 不想在使用 fragment 时逐渐包装内容