Android數(shù)據(jù)庫中怎么存取圖片

Android 數(shù)據(jù)庫中怎么存取圖片,針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),古塔企業(yè)網(wǎng)站建設(shè),古塔品牌網(wǎng)站建設(shè),網(wǎng)站定制,古塔網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,古塔網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。


一、數(shù)據(jù)庫創(chuàng)建和存取方法:

在數(shù)據(jù)庫創(chuàng)建時(shí),圖片字段的數(shù)據(jù)類型存儲為 BLOB數(shù)據(jù)庫插入操作

public void onCreate(SQLiteDatabase db)
{

    String sql = "create table " + TB_NAME + " ( " + ID + " integer primary key , " + IMAGE + " BLOB ) ";
    db.execSQL(sql);

}


將圖片一字節(jié)形式存儲數(shù)據(jù)庫讀取操作

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ù)庫中怎么存取圖片問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

文章標(biāo)題:Android數(shù)據(jù)庫中怎么存取圖片
本文URL:http://muchs.cn/article16/gdcjdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、定制開發(fā)靜態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司