Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android將圖片轉換存到數據庫再從數據庫讀取轉換成圖片實現代碼

android將圖片轉換存到數據庫再從數據庫讀取轉換成圖片實現代碼

編輯:關於Android編程

首先,我們要把圖片存入到數據庫中,首先要創建一個數據庫, 如下所示:

復制代碼 代碼如下:
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 {

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

    private Context mContext;

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

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

    //創建表並初始化表
    @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);
    }

    //將轉換後的圖片存入到數據庫中
    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轉換成可以用來存儲的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();
    }

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


代碼注釋的比較詳細.

這裡重點要說的是初始化數據庫的時候,將Drawable轉變成byte[]的時候,先講Drawable轉換成Bitmap,然後將Bitmap存入字節數據輸出流,從輸出流裡獲取byte[]數組。

復制代碼 代碼如下:
ByteArrayOutputStream os = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, os);
return os.toByteArray();

之後將字符數組存入到類型為blob的數據庫中去。
復制代碼 代碼如下:
ContentValues cv = new ContentValues();
cv.put(PictureColumns.PICTURE, getPicture(drawable));
db.insert(TABLE_NAME, null, cv);

之後在代碼中從數據庫中取出byte[],然後轉換成Drawable,設置圖片即可。

代碼如下:

復制代碼 代碼如下:
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>();

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

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

重點注意如何將數據庫中取出的byte[]轉換成drawable:
復制代碼 代碼如下:
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length, null);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
Drawable drawable = bitmapDrawable;

 

運行效果如下:

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved