一、數據庫創建和存取方法:
在數據庫創建時,圖片字段的數據類型存儲爲 BLOB數據庫插入操作
public void onCreate(SQLiteDatabase db)
{
String sql = "create table " + TB_NAME + " ( " + ID + " integer primary key , " + IMAGE + " BLOB ) ";}
db.execSQL(sql);
將圖片一字節形式存儲數據庫讀取操作
public long insert(byte[] img)
{
SQLiteDatabase db = getWritableDatabase();}
ContentValues cv = new ContentValues();
cv.put(IMAGE, img);
long result = db.insert(TB_NAME, null, cv);
return result;
得到的圖片是Bitmap類型,參數position 點擊圖片在ListView、GridView等內的位置 ,可根據需要編寫代碼二
public Bitmap getBmp(int position)
{
SQLiteDatabase db = getReadableDatabase();}
Cursor cursor = select(TB_NAME);
cursor.moveToPosition(position);
byte[] in = cursor.getBlob(cursor.getColumnIndex(IMAGE));
Bitmap bmpout = BitmapFactory.decodeByteArray(in, 0, in.length);
return bmpout;
二、將圖片轉化爲 byte[]//參數id爲圖片資源 (R.drawable.img)
public byte[] img(int id)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(id)).getBitmap();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}