- 浏览: 133598 次
- 性别:
文章分类
最新评论
Android使用SQLiteDatabase直接存取数据与图像
- 博客分类:
- Android
Android使用SQLiteDatabase直接存取数据与图像的简单方法如下:
package com.test; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import com.test.R; import android.app.Activity; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Bitmap.Config; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class SQLiteDatabaseTest extends Activity { /** Called when the activity is first created. */ private Button btnSave; private Button btnLoad; private Button btnClear; private ImageView imgView; private ImageView imgView2; private TextView txtView; private Bitmap bmp; private static SQLiteDatabase db; private Context mContext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnSave = (Button) findViewById(R.id.btnSave); btnLoad = (Button) findViewById(R.id.btnLoad); btnClear = (Button) findViewById(R.id.btnClear); imgView = (ImageView) findViewById(R.id.imgView); imgView2 = (ImageView) findViewById(R.id.imgView2); txtView = (TextView) findViewById(R.id.txtView); btnSave.setOnClickListener(new ClickEvent()); btnLoad.setOnClickListener(new ClickEvent()); btnClear.setOnClickListener(new ClickEvent()); imgView2.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bg)); mContext = SQLiteDatabaseTest.this; // 创建数据库文件 File path = mContext.getDir("databases", Context.MODE_WORLD_WRITEABLE); path = new File(path, "test.db"); int flag = SQLiteDatabase.OPEN_READWRITE; flag = flag | SQLiteDatabase.CREATE_IF_NECESSARY; flag = flag | SQLiteDatabase.NO_LOCALIZED_COLLATORS; db = SQLiteDatabase.openDatabase(path.getAbsolutePath(), null, flag); // 创建表 String sql = "create table if not exists info(" + "id integer primary key autoincrement,name varchar(20)," + "time varchar(20),img BLOB)"; db.execSQL(sql); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if (db.isOpen()) { db.close(); } if(!bmp.isRecycled()){ bmp.recycle(); } } class ClickEvent implements View.OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub if (v == btnSave) { Cursor c = db.rawQuery("select * from info", null); ContentValues values = new ContentValues(); c.moveToFirst(); values.put("name", "test" + (c.getCount() + 1)); SimpleDateFormat sDateFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); String time = sDateFormat.format(new java.util.Date()); values.put("time", time); Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.bg); if (null != bmp) { //在Bitmap上绘制标签 Bitmap drawBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Config.ARGB_8888); Canvas cvs = new Canvas(drawBmp); Paint p = new Paint(); p.setColor(Color.RED); p.setTextSize(22); cvs.drawBitmap(bmp, 0, 0, p); cvs.drawText("test" + (c.getCount() + 1), 10, 20,p); //将绘制后Bitmap转为Byte[]并加入values values.put("img", bmpToByteArray(drawBmp)); drawBmp.recycle(); } db.insert("info", null, values); c.close(); bmp.recycle(); } else if (v == btnLoad) { Cursor c = db.rawQuery("select * from info", null); c.moveToLast(); if (c.isLast()) { String name = c.getString(c.getColumnIndex("name")); txtView.setText("name:" + name + " 共计:" + c.getCount() + " 条"); bmp = cursorToBmp(c, c.getColumnIndex("img")); imgView.setImageBitmap(bmp); } c.close(); } else if (v == btnClear) { imgView.setImageBitmap(null); } } } // Bitmap to byte[] public byte[] bmpToByteArray(Bitmap bmp) { // Default size is 32 bytes ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos); bos.close(); } catch (IOException e) { e.printStackTrace(); } return bos.toByteArray(); } // Cursor to bitmap Bitmap cursorToBmp(Cursor c, int columnIndex) { byte[] data = c.getBlob(columnIndex); try { return BitmapFactory.decodeByteArray(data, 0, data.length); } catch (Exception e) { return null; } } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <ImageView android:id="@+id/imgView" android:layout_width="640dip" android:layout_height="fill_parent"/> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="150dip" android:layout_height="fill_parent" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:orientation="vertical"> <ImageView android:id="@+id/imgView2" android:layout_width="fill_parent" android:layout_height="150dip" android:layout_marginTop="10dip"/> <Button android:id="@+id/btnSave" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="保存图像"/> <Button android:id="@+id/btnLoad" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="载入图像"/> <Button android:id="@+id/btnClear" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="清除图像"/> <TextView android:id="@+id/txtView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:textSize="22sp" android:text="..."/> </LinearLayout> </LinearLayout>
开发环境:XP3+Eclipse+Android2.2+JDK6.0
测试环境:Android2.2,5寸屏,分辨率640X480
源代码:http://download.csdn.net/detail/xinzheng_wang/4420817
发表评论
-
Android onTouchEvent, onClick及onLongClick的调用机制
2012-03-08 15:44 853针对屏幕上的一个View控件,Android如何区分应当触发o ... -
android 常用颜色表
2012-03-09 16:01 859<?xml version="1.0" ... -
android ndk 环境搭建及基本编程思路
2012-03-28 16:09 847本文主要是介绍一下android ndk环境搭建及基本编程思路 ... -
windows下eclipse android-ndkr7b环境配置
2012-03-31 19:49 869注意:android-ndkr7b版本中已经集成了cyg ... -
配置 eclipse ndk 环境遇到的问题
2012-04-01 08:52 945困惑了两天的环境配置问题,按照网上提供的方法去做总是错误 ... -
Android 配置 OpenCV2.3.1
2012-04-01 17:56 1983OpenCV2.3.1-android中大部分重要的AP ... -
Android中dip与px之间单位转换
2012-04-11 16:10 690/** * 根据手机的分辨率从dip 的单位转成为px(像素 ... -
Android 自定义像素AVD模拟器无键盘
2012-04-18 14:26 3359在Android自定义像素(如:800X480)时,AV ... -
在Android虚拟机AVD中安装APK
2012-04-18 15:13 2579为了方便测试,有时候会需要在自定义的Android虚拟机A ... -
Android SurfaceView onDraw()绘图问题
2012-06-29 11:32 9935在继承SurfaceView的类中即使重写了onDraw ... -
Android 自定义渐变背景
2012-07-04 08:36 1207在Eclipse的Android工程的res下建立dra ... -
Android 隐藏系统状态栏和标题栏
2012-07-04 09:06 1178Android中若想 隐藏系统状态栏和标题栏(全屏显示)的 ... -
Android双SurfaceView实现底部拍照,顶部绘图
2012-07-04 10:36 1478当SurfaceHolder对象的类型设置为Surf ... -
Android数据的四种存储方式之SharedPreferences、SQLite、ContentProvider和File
2012-07-06 15:32 2713Android系统一共提供了四种数据存储方式,分别 ... -
Android SQLite存取图像
2012-07-06 18:36 904Android SQLite存取图像的简单方法如下: ... -
Android 继承SQLiteOpenHelper自定义DBHelper存取数据与图像
2012-07-10 15:07 1339Android 继承SQLiteOpenHelper自定 ... -
Android 系统菜单与自定义菜单
2012-07-25 16:28 2183Android 系统菜单与自 ...
相关推荐
因为数据库的操作在开发中的重要性是不言而喻的,所以,在数据库这一方面下了很大的功夫,终于掌握了基本的操作,包括建表和删除,对数据库的操作,“增删改查”基本操作,而且用了封装性来实现,是学习数据库的一个...
Android 数据库SQLiteDatabase的使用 1.数据库辅助操作接口SQLiteDatabaseOpenHelper的使用 2.ContentValues的使用 3.数据的增删改查 4.listView 与Adapter的使用 5.OptionsMenu的使用 6.Editext隐藏失去焦点的方法
android开发sqliteDataBase使用实例
Android SQLiteDatabase的使用详解
这是android的SQLiteDatabase的CRUD小案例,做了一些关于android连接SQLiteDabase的基础案例
实现了安卓移动应用开发中数据库的所有基本功能,完整直观实现了一个数据库的应用。
主要以一个简单的实例为大家详细讲解Android中SQLiteDatabase使用方法,感兴趣的小伙伴们可以参考一下
使用sqlitedatabase的android api可以被描述为不方便和丑陋(至少对我来说是这样)。这个库倾向于使使用它更容易,并减少您可能用来编写的样板代码的数量。
一个小例子,操作 android SQLitedatabase
使用 SQLiteDatabase 操作 SQLite 数据库 代码如下:/* Android提供了一个名为SQLiteDatabase的类,该类封装了一些操作数据库的API,使用该类可以完成对数据进行添加(Create)、查询(Retrieve)、更新(Update)和删除...
SQLiteDataBase简单Android实例 如果没积分的朋友,请到http://blog.csdn.net/zml_2015 评论,留下自己的邮箱哦
android 数据库操作 ,数据的增删改查,以及andorid布局页面的编写
NULL 博文链接:https://phenom.iteye.com/blog/678400
android数据库使用真的很不错 SQLiteOpenHelper 创建数据库和数据库版本管理的辅助类。这是一个抽象类,所以我们一般都有一个SQLiteOpenHelper子类,需要继承实现 • void onCreate(SQLiteDatabase db) 在数据库第...
listView以及SQLite的整合,通过两种的结合实现相应的添加、查询等功能.
Android高手进阶教程之----Android 数据库SQLiteDatabase的使用!.doc Android高手进阶教程之----Android 通用获取Ip的方法(判断手机是否联网的方法)!!!.doc Android高手进阶教程之----Android 在一个应用中如何...
NULL 博文链接:https://410063005.iteye.com/blog/1567413