sqlite应用【android菜鸟修行记(二)】8.30.2013

欢迎关注我的微信公众账号“90后萌呆小怪兽


无组织,有纪律,爱创造,爱自由

“呆萌贱坏怪”是我们的style

让我们像小怪兽一样,用我们的方式,思维一起去颠覆这个世界吧



刚刚学习的android的数据存储教程中介绍了3种存储方法,一种是xml存储,一种是面向对象式的db4o数据库,一种是关系型数据库sqlite。作为初学者不太好总结三种数据库的优劣,在网上看到了有关前两种数据库的对比:【xml数据库与db4o的简要对比】http://www.cnblogs.com/chenxizhang/archive/2009/08/11/1543908.html

xml存储是利用android中的SharePreferences方法将数据存储到xml文件中,可以存储boolean,String,float,long,int 5种数据类型存放位置为/data/data/<包名>/shared_prefs/存储的xml文件,一般用来存储字体大小,语言类型,游戏得分,登陆时间等。在eoe社区找到的一个demo:https://github.com/c123853648/android_xmlSave1

sqlite数据库是一种轻量级的关系型数据库,根据教程,今天写了一个demo。运行画面如下


第一步,实现SQLiteOpenHelper这个抽象类

public class MySQLiteOpenHelper extends SQLiteOpenHelper {

	public MySQLiteOpenHelper(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}



	@Override
	public void onCreate(SQLiteDatabase arg0) {
		// TODO Auto-generated method stub
		arg0.execSQL("create table imagetable (_id INTEGER PRIMARY KEY AUTOINCREMENT,image BLOB)");

	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}

}
在onCreate方法中创建数据表imagetable。

第二步在主类中创建数据库“saveimage”

<span>		</span>mySQLiteOpenHelpe=new MySQLiteOpenHelper(this, "saveimage.db", null, 1);
<span>		</span>mydb=mySQLiteOpenHelpe.getWritableDatabase();
MySQLiteOpenHelper的构造方法中的几个参数说明:

public SQLiteOpenHelper (Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
contextto use to open or create the database
nameof the database file, or null for an in-memory database
factoryto use for creating cursor objects, or null for the default
versionnumber of the database (starting at 1); if the database is older, onUpgrade(SQLiteDatabase, int, int) will be used to upgrade the database

第三步 将图片转换为位图

Bitmap bitmap1=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
public static Bitmap decodeResource (Resources res, int id)

将现有的输入资源转换为位图

Parameters

resThe resources object containing the image data
idThe resource id of the image data
第四步 将位图转换为字节数组
int size=bitmap1.getWidth()*bitmap1.getHeight()*4; 
ByteArrayOutputStream baos=new ByteArrayOutputStream(size);  //构建一个字节数组输出流大小为size
bitmap1.compress(Bitmap.CompressFormat.PNG, 100, baos);<span>	</span>     //设置位图压缩格式为PNG,质量100%。输出流为baos
byte[] imagedata1=baos.toByteArray();<span>			</span>     //将字节数组输出流转换为字节数组
第五步 将字节数组保存到数据库中
ContentValues cv=new ContentValues();
				cv.put("_id", 1);
				cv.put("image", imagedata1);
				mydb.insert("imagetable", null, cv);
				iv1.setImageDrawable(null);
				try {
					baos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

到此保存过程已经完成,可以在eclipse中的File explorer中查看数据库

红色便是我们保存数据库,点击后再点击绿色的按钮可以将数据库从虚拟机上导出到windows中,在利用SQLite Expert 可以查看数据库中的内容如图


查询数据时,过程相反:

	Cursor cur=mydb.query("imagetable", new String[] {"_id","image"} , null, null, null, null, null);
				byte[] imagequery=null;
				if(cur.moveToNext()){
					imagequery=cur.getBlob(cur.getColumnIndex("image"));
				}
				Bitmap imagebitmap=BitmapFactory.decodeByteArray(imagequery, 0, imagequery.length);
				iv1.setImageBitmap(imagebitmap);


源码见:https://github.com/c123853648/android_saveImage1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值