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 数据库SQLiteDatabase的使用 1.数据库辅助操作接口SQLiteDatabaseOpenHelper的使用 2.ContentValues的使用 3.数据的增删改查 4.listView 与Adapter的使用 5.OptionsMenu的使用 6.Editext隐藏失去焦点的方法
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
第1章 Android应用与开发环境 1 1.1 Android的发展和历史 2 1.1.1 Android的发展和简介 2 1.1.2 Android平台架构及特性 3 1.2 搭建Android开发环境 5 1.2.1 下载和安装Android SDK 5 1.2.2 安装...