android將圖片轉(zhuǎn)換存到數(shù)據(jù)庫再從數(shù)據(jù)庫讀取轉(zhuǎn)換成圖片實現(xiàn)代碼
首先,我們要把圖片存入到數(shù)據(jù)庫中,首先要創(chuàng)建一個數(shù)據(jù)庫, 如下所示:
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 {
//數(shù)據(jù)庫的字段
public static class PictureColumns implements BaseColumns {
public static final String PICTURE = "picture";
}
private Context mContext;
//數(shù)據(jù)庫名
private static final String DATABASE_NAME = "picture.db";
//數(shù)據(jù)庫版本號
private static final int DATABASE_Version = 1;
//表名
private static final String TABLE_NAME = "picture";
//創(chuàng)建數(shù)據(jù)庫
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ù)庫中
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)換成可以用來存儲的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ù)庫
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = " DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
}
代碼注釋的比較詳細.
這里重點要說的是初始化數(shù)據(jù)庫的時候,將Drawable轉(zhuǎn)變成byte[]的時候,先講Drawable轉(zhuǎn)換成Bitmap,然后將Bitmap存入字節(jié)數(shù)據(jù)輸出流,從輸出流里獲取byte[]數(shù)組。
ByteArrayOutputStream os = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, os);
return os.toByteArray();
之后將字符數(shù)組存入到類型為blob的數(shù)據(jù)庫中去。
ContentValues cv = new ContentValues();
cv.put(PictureColumns.PICTURE, getPicture(drawable));
db.insert(TABLE_NAME, null, cv);
之后在代碼中從數(shù)據(jù)庫中取出byte[],然后轉(zhuǎn)換成Drawable,設(shè)置圖片即可。
代碼如下:
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>();
//查詢數(shù)據(jù)庫
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;
}
}
重點注意如何將數(shù)據(jù)庫中取出的byte[]轉(zhuǎn)換成drawable:
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length, null);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
Drawable drawable = bitmapDrawable;
運行效果如下:
- Android SQLite數(shù)據(jù)庫增刪改查操作的使用詳解
- Android使用SQLite數(shù)據(jù)庫的簡單實例
- android創(chuàng)建數(shù)據(jù)庫(SQLite)保存圖片示例
- Android 用adb pull或push 拷貝手機文件到到電腦上,拷貝手機數(shù)據(jù)庫到電腦上,拷貝電腦數(shù)據(jù)庫到手機上
- Android中的SQL查詢語句LIKE綁定參數(shù)問題解決辦法(sqlite數(shù)據(jù)庫)
- 條件數(shù)據(jù)庫Android:sqllite的簡單使用
- Android學(xué)習筆記-保存數(shù)據(jù)到SQL數(shù)據(jù)庫中(Saving Data in SQL Databases)
- android實現(xiàn)raw文件夾導(dǎo)入數(shù)據(jù)庫代碼
- Android中操作SQLite數(shù)據(jù)庫快速入門教程
- Android數(shù)據(jù)庫操作工具類分享
相關(guān)文章
Android 開發(fā)調(diào)試工具的使用總結(jié)
這篇文章主要介紹了Android 開發(fā)調(diào)試工具的使用總結(jié)的相關(guān)資料,這里整理了,Android開發(fā)所需要的常用工具,需要的朋友可以參考下2016-11-11Android ListView數(shù)據(jù)綁定顯示的三種解決方法
本篇文章小編為大家介紹,Android ListView數(shù)據(jù)綁定顯示的三種解決方法。需要的朋友參考下2013-04-04Android?autojs隨時翻譯剪貼板單詞實現(xiàn)示例
這篇文章主要為大家介紹了Android?autojs隨時翻譯剪貼板單詞,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09viewpager+photoview實現(xiàn)圖片查看器
這篇文章主要為大家詳細介紹了viewpager+photoview實現(xiàn)圖片查看器,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12