欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android將圖片轉(zhuǎn)換存到數(shù)據(jù)庫再從數(shù)據(jù)庫讀取轉(zhuǎn)換成圖片實現(xiàn)代碼

 更新時間:2013年11月26日 16:05:04   作者:  
有時候我們想把圖片存入到數(shù)據(jù)庫中,盡管這不是一種明智的選擇,但有時候還是不得以會用到,下面說說將圖片轉(zhuǎn)換成byte[]數(shù)組存入到數(shù)據(jù)庫中去,并從數(shù)據(jù)庫中取出來轉(zhuǎn)換成圖像顯示出來

首先,我們要把圖片存入到數(shù)據(jù)庫中,首先要創(chuàng)建一個數(shù)據(jù)庫, 如下所示:

復(fù)制代碼 代碼如下:

package com.android.test;

import java.io.ByteArrayOutputStream;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.provider.BaseColumns;

public class PictureDatabase extends SQLiteOpenHelper {

    //數(shù)據(jù)庫的字段
    public static class PictureColumns implements BaseColumns {
        public static final String PICTURE = "picture";
    }

    private Context mContext;

    //數(shù)據(jù)庫名
    private static final String DATABASE_NAME = "picture.db";
    //數(shù)據(jù)庫版本號
    private static final int DATABASE_Version = 1;
    //表名
    private static final String TABLE_NAME = "picture";

    //創(chuàng)建數(shù)據(jù)庫
    public PictureDatabase (Context context) {
        super(context, DATABASE_NAME, null, DATABASE_Version);
        this.mContext = context;
    }

    //創(chuàng)建表并初始化表
    @Override
    public void onCreate (SQLiteDatabase db) {
        String sql = "Create table " + TABLE_NAME + "(" + BaseColumns._ID
        + " integer primary key autoincrement," + PictureColumns.PICTURE
        + " blob not null);";
        db.execSQL(sql);

        //初始化
        initDataBase(db,mContext);
    }

    //將轉(zhuǎn)換后的圖片存入到數(shù)據(jù)庫中
    private void initDataBase (SQLiteDatabase db, Context context) {
        Drawable drawable = context.getResources().getDrawable(R.drawable.test_icon_resizer);
        ContentValues cv = new ContentValues();
        cv.put(PictureColumns.PICTURE, getPicture(drawable));
        db.insert(TABLE_NAME, null, cv);
    }

    //將drawable轉(zhuǎn)換成可以用來存儲的byte[]類型
    private byte[] getPicture(Drawable drawable) {
        if(drawable == null) {
            return null;
        }
        BitmapDrawable bd = (BitmapDrawable) drawable;
        Bitmap bitmap = bd.getBitmap();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 100, os);
        return os.toByteArray();
    }

    //更新數(shù)據(jù)庫
    @Override
    public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = " DROP TABLE IF EXISTS " + TABLE_NAME;
        db.execSQL(sql);
        onCreate(db);
    }
}


代碼注釋的比較詳細.

這里重點要說的是初始化數(shù)據(jù)庫的時候,將Drawable轉(zhuǎn)變成byte[]的時候,先講Drawable轉(zhuǎn)換成Bitmap,然后將Bitmap存入字節(jié)數(shù)據(jù)輸出流,從輸出流里獲取byte[]數(shù)組。

復(fù)制代碼 代碼如下:

ByteArrayOutputStream os = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, os);
return os.toByteArray();

之后將字符數(shù)組存入到類型為blob的數(shù)據(jù)庫中去。

復(fù)制代碼 代碼如下:

ContentValues cv = new ContentValues();
cv.put(PictureColumns.PICTURE, getPicture(drawable));
db.insert(TABLE_NAME, null, cv);

之后在代碼中從數(shù)據(jù)庫中取出byte[],然后轉(zhuǎn)換成Drawable,設(shè)置圖片即可。

代碼如下:

復(fù)制代碼 代碼如下:

package com.android.test;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;

public class TestPicture extends Activity {

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImageView iv = new ImageView(this);
        if(getDrawable().size() != 0) {
            iv.setImageDrawable(getDrawable().get(0));
        }
        setContentView(iv);
    }

   
    private ArrayList<Drawable> getDrawable() {
        PictureDatabase pd = new PictureDatabase(this);
        SQLiteDatabase sd = pd.getWritableDatabase();

        ArrayList<Drawable> drawables = new ArrayList<Drawable>();

        //查詢數(shù)據(jù)庫
        Cursor c = sd.query("picture", null, null, null, null, null, null);

        //遍歷數(shù)據(jù)
        if(c != null && c.getCount() != 0) {
            while(c.moveToNext()) {
                //獲取數(shù)據(jù)
                byte[] b = c.getBlob(c.getColumnIndexOrThrow(PictureDatabase.PictureColumns.PICTURE));
                //將獲取的數(shù)據(jù)轉(zhuǎn)換成drawable
                Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length, null);
                BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
                Drawable drawable = bitmapDrawable;
                drawables.add(drawable);
            }
        }
        return drawables;
    }
}

重點注意如何將數(shù)據(jù)庫中取出的byte[]轉(zhuǎn)換成drawable:

復(fù)制代碼 代碼如下:

Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length, null);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
Drawable drawable = bitmapDrawable;

 

運行效果如下:

相關(guān)文章

最新評論