首先,我們要把圖片存入到數(shù)據(jù)庫(kù)中,首先要?jiǎng)?chuàng)建一個(gè)數(shù)據(jù)庫(kù), 如下所示:
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ù)庫(kù)的字段
public static class PictureColumns implements BaseColumns {
public static final String PICTURE = "picture";
}
private Context mContext;
//數(shù)據(jù)庫(kù)名
private static final String DATABASE_NAME = "picture.db";
//數(shù)據(jù)庫(kù)版本號(hào)
private static final int DATABASE_Version = 1;
//表名
private static final String TABLE_NAME = "picture";
//創(chuàng)建數(shù)據(jù)庫(kù)
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ù)庫(kù)中
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)換成可以用來(lái)存儲(chǔ)的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ù)庫(kù)
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = " DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
}
代碼注釋的比較詳細(xì).
這里重點(diǎn)要說(shuō)的是初始化數(shù)據(jù)庫(kù)的時(shí)候,將Drawable轉(zhuǎn)變成byte[]的時(shí)候,先講Drawable轉(zhuǎn)換成Bitmap,然后將Bitmap存入字節(jié)數(shù)據(jù)輸出流,從輸出流里獲取byte[]數(shù)組。
之后將字符數(shù)組存入到類型為blob的數(shù)據(jù)庫(kù)中去。
之后在代碼中從數(shù)據(jù)庫(kù)中取出byte[],然后轉(zhuǎn)換成Drawable,設(shè)置圖片即可。
代碼如下:
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ù)庫(kù)
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;
}
}
重點(diǎn)注意如何將數(shù)據(jù)庫(kù)中取出的byte[]轉(zhuǎn)換成drawable:
運(yùn)行效果如下:
新聞熱點(diǎn)
疑難解答
圖片精選