如何存储图像的BLOB SQLite中和放大器;如何找回? [英] how to store Image as blob in Sqlite & how to retrieve it?

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

问题描述

我要存储的图像(从URL)到SQLite数据库。

I want to store an image(from url) into a sqlite database.

有关,我使用:

db = new DataBase(getApplicationContext());
URL url = new URL("http://sree.cc/wp-content/uploads/schogini_team.png");
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is,128);
ByteArrayBuffer barb= new ByteArrayBuffer(128);

int current = 0;
while ((current = bis.read()) != -1) {
    barb.append((byte) current);
}

ContentValues filedata= new ContentValues();

filedata.put(DataBase.IMG_SRC,barb.toByteArray());

db.insert(DataBase.Table_Img, null, filedata);

插入()

public void insert(String tableImg, Object object,
        ContentValues dataToInsert) {
    // TODO Auto-generated method stub
    String sql = "INSERT INTO "+tableImg+" ("+ID+","+IMG_SRC+") " +
            "VALUES ('"+1+"','"+dataToInsert+"')";
    db.execSQL(sql);
}

有关图像的检索:

Cursor cursor = db.selectDataToShow(DataBase.Table_Img, DataBase.IMG_SRC);

byte[] imageByteArray=cursor.getBlob(cursor.getColumnIndex(DataBase.IMG_SRC));      
cursor.close();

ByteArrayInputStream imageStream = new ByteArrayInputStream(imageByteArray);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);

System.out.println(">>>>>>>>>>>>>>>>>>>>>> "+theImage);

所以我在这里得到了

和我的数据库中的图像的值存储为:图片= [B @ 43e5ac48]

And in my database the value of image stored as: Image=[B@43e5ac48]

推荐答案

下面用我的应用程序中的codeI

Here the code i used for my app

这code会从URL中的图像,并转换为字节数组

This code will take a image from url and convert is to a byte array

byte[] logoImage = getLogoImage(IMAGEURL);

private byte[] getLogoImage(String url){
     try {
             URL imageUrl = new URL(url);
             URLConnection ucon = imageUrl.openConnection();

             InputStream is = ucon.getInputStream();
             BufferedInputStream bis = new BufferedInputStream(is);

             ByteArrayBuffer baf = new ByteArrayBuffer(500);
             int current = 0;
             while ((current = bis.read()) != -1) {
                     baf.append((byte) current);
             }

             return baf.toByteArray();
     } catch (Exception e) {
             Log.d("ImageManager", "Error: " + e.toString());
     }
     return null;
}

要保存图片到数据库我用这个code。

To save the image to db i used this code.

    public void insertUser(){
    SQLiteDatabase db               =   dbHelper.getWritableDatabase();

    String delSql                       =   "DELETE FROM ACCOUNTS";
    SQLiteStatement delStmt         =   db.compileStatement(delSql);
    delStmt.execute();

    String sql                      =   "INSERT INTO ACCOUNTS (account_id,account_name,account_image) VALUES(?,?,?)";
    SQLiteStatement insertStmt      =   db.compileStatement(sql);
    insertStmt.clearBindings();
    insertStmt.bindString(1, Integer.toString(this.accId));
    insertStmt.bindString(2,this.accName);
    insertStmt.bindBlob(3, this.accImage);
    insertStmt.executeInsert();
    db.close();
}

要检索图像回用,这是codeI。

To retrieve the image back this is code i used.

public Account getCurrentAccount() {
    SQLiteDatabase db       =   dbHelper.getWritableDatabase();
    String sql              =   "SELECT * FROM ACCOUNTS";
    Cursor cursor           =   db.rawQuery(sql, new String[] {});

    if(cursor.moveToFirst()){
        this.accId             = cursor.getInt(0);
        this.accName           = cursor.getString(1);
        this.accImage          = cursor.getBlob(2);
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    db.close();
    if(cursor.getCount() == 0){
        return null;
    } else {
        return this;
    }
}

最后加载这个图片给ImageView的

Finally to load this image to a imageview

logoImage.setImageBitmap(BitmapFactory.decodeByteArray( currentAccount.accImage, 
        0,currentAccount.accImage.length));

这篇关于如何存储图像的BLOB SQLite中和放大器;如何找回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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