android創(chuàng)建數(shù)據(jù)庫(SQLite)保存圖片示例
//1.創(chuàng)建數(shù)據(jù)庫
public class DBService extends SQLiteOpenHelper {
private final static int VERSION = 1;
private final static String DATABASE_NAME = "uniteqlauncher.db";
public DBService(Context context) {
this(context, DATABASE_NAME, null, VERSION);
}
public DBService(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE [launcher]("
+ "[_id] INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "[photo] BINARY)"; //保存為binary格式
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion > oldVersion){
db.execSQL("DROP TABLE IF EXISTS[launcher]");
} else {
return;
}
onCreate(db);
}
}
//保存圖片到數(shù)據(jù)庫
public void savePhoto(Drawable appIcon, Context mContext){
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.app_view, null);
ImageView iv = (ImageView) v.findViewById(R.id.appicon);
iv.setImageDrawable(appIcon);
String INSERT_SQL = "INSERT INTO launcher(photo) values(?)";
SQLiteDatabase db = mDBService.getWritableDatabase(); // 得到數(shù)據(jù)庫
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
((BitmapDrawable) iv.getDrawable()).getBitmap().compress(
CompressFormat.PNG, 100, baos);//壓縮為PNG格式,100表示跟原圖大小一樣
Object[] args = new Object[] {baos.toByteArray() };
db.execSQL(INSERT_SQL, args);
baos.close();
db.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//3.從數(shù)據(jù)庫中取圖片
public void getPhoto() {
String SELECT_SQL = "SELECT photo FROM launcher";
ImageView appIcon = (ImageView) v.findViewById(R.id.appicon);//v是我在類中定義的一個(gè)view對(duì)象,跟前面保存圖片一樣
byte[] photo = null;
mDBService = new DBService(getContext());
SQLiteDatabase db = mDBService.getReadableDatabase();
Cursor mCursor = db.rawQuery(SELECT_SQL, null);
if (mCursor != null) {
if (mCursor.moveToFirst()) {//just need to query one time
photo = mCursor.getBlob(mCursor.getColumnIndex("photo"));//取出圖片
}
}
if (mCursor != null) {
mCursor.close();
}
db.close();
ByteArrayInputStream bais = null;
if (photo != null) {
bais = new ByteArrayInputStream(photo);
appIcon.setImageDrawable(Drawable.createFromStream(bais, "photo"));//把圖片設(shè)置到ImageView對(duì)象中
}
//appIcon顯示的就是之前保存到數(shù)據(jù)庫中的圖片
}
- DJango的創(chuàng)建和使用詳解(默認(rèn)數(shù)據(jù)庫sqlite3)
- Python操作SQLite數(shù)據(jù)庫的方法詳解【導(dǎo)入,創(chuàng)建,游標(biāo),增刪改查等】
- swift3.0 創(chuàng)建sqlite數(shù)據(jù)庫步驟方法
- C#操作SQLite數(shù)據(jù)庫方法小結(jié)(創(chuàng)建,連接,插入,查詢,刪除等)
- PHP+sqlite數(shù)據(jù)庫操作示例(創(chuàng)建/打開/插入/檢索)
- Android創(chuàng)建和使用數(shù)據(jù)庫SQLIte
- SQLite 創(chuàng)建數(shù)據(jù)庫實(shí)例操作
相關(guān)文章
Android 連接Wifi和創(chuàng)建Wifi熱點(diǎn)的實(shí)例
本篇文章介紹了Android 連接Wifi和創(chuàng)建Wifi熱點(diǎn),小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧。2016-10-10
Android控件FlowLikeView實(shí)現(xiàn)點(diǎn)贊動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了一個(gè)點(diǎn)贊動(dòng)畫的優(yōu)雅控件FlowLikeView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android Studio中CodeStyle模板的配置方式
這篇文章主要介紹了Android Studio中CodeStyle模板的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android性能圖論在啟動(dòng)優(yōu)化中的應(yīng)用示例詳解
這篇文章主要為大家介紹了Android性能圖論在啟動(dòng)優(yōu)化中的應(yīng)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
基于Socket.IO實(shí)現(xiàn)Android聊天功能代碼示例
本篇文章主要介紹了基于Socket.IO實(shí)現(xiàn)Android聊天功能代碼示例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08

