加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-12 20:12:50 所属栏目:百科 来源:网络整理
导读:欢迎关注我的微信公众账号“ 90后萌呆小怪兽 ” 无组织,有纪律,爱创造,爱自由 “呆萌贱坏怪”是我们的style 让我们像小怪兽一样,用我们的方式,思维一起去颠覆这个世界吧 刚刚学习的android的数据存储教程中介绍了3种存储方法,一种是xml存储,一种是面
to use to open or create the database of the database file,or null for an in-memory database to use for creating cursor objects,or null for the default number 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 staticBitmapdecodeResource(Resourcesres,int id)

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

Parameters

The resources object containing the image data The 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",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);
				byte[] imagequery=null;
				if(cur.moveToNext()){
					imagequery=cur.getBlob(cur.getColumnIndex("image"));
				}
				Bitmap imagebitmap=BitmapFactory.decodeByteArray(imagequery,imagequery.length);
				iv1.setImageBitmap(imagebitmap);
源码见:https://github.com/c123853648/android_saveImage1

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

欢迎关注我的微信公众账号“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的构造方法中的几个参数说明:

publicSQLiteOpenHelper(Contextcontext,Stringname,SQLiteDatabase.CursorFactoryfactory,int version)

contextnamefactoryversion
resid
    推荐文章
      热点阅读