大佬教程收集整理的这篇文章主要介绍了android创建数据库(SQLite)保存图片示例,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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格式
}
@Override
public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
if(newVersion > oldVersion){
db.execsql("drop table IF EXISTS[launcher]");
} else {
return;
}
onCreate(db);
}
}
//保存图片到数据库
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(); // 得到数据库
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.从数据库中取图片
public void getPhoto() {
String SELECT_sql = "SELECT photo FROM launcher";
ImageView appIcon = (ImageView) v.findViewById(R.id.appicon);//v是我在类中定义的一个view对象,跟前面保存图片一样
byte[] photo = null;@H_619_6@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) {@H_619_6@mcursor.close();
}
db.close();
ByteArrayInputStream bais = null;
if (photo != null) {
bais = new ByteArrayInputStream(photo);
appIcon.setImageDrawable(Drawable.createFromStream(bais,"photo"));//把图片设置到ImageView对象中
}
//appIcon显示的就是之前保存到数据库中的图片
}
以上是大佬教程为你收集整理的android创建数据库(SQLite)保存图片示例全部内容,希望文章能够帮你解决android创建数据库(SQLite)保存图片示例所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。