您好,登錄后才能下訂單哦!
Android 數(shù)據(jù)庫(kù)中怎么存取圖片,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
一、數(shù)據(jù)庫(kù)創(chuàng)建和存取方法:
在數(shù)據(jù)庫(kù)創(chuàng)建時(shí),圖片字段的數(shù)據(jù)類型存儲(chǔ)為 BLOB數(shù)據(jù)庫(kù)插入操作
public void onCreate(SQLiteDatabase db) { String sql = "create table " + TB_NAME + " ( " + ID + " integer primary key , " + IMAGE + " BLOB ) "; db.execSQL(sql); }
將圖片一字節(jié)形式存儲(chǔ)數(shù)據(jù)庫(kù)讀取操作
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類型,參數(shù)position 點(diǎn)擊圖片在ListView、GridView等內(nèi)的位置 ,可根據(jù)需要編寫代碼二
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; }
二、將圖片轉(zhuǎn)化為 byte[]//參數(shù)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(); }
關(guān)于Android 數(shù)據(jù)庫(kù)中怎么存取圖片問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。