赞
踩
Android数据库中的创建,图片的存、取操作如下:
数据库类:
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
-
- /**
- * 此类继承了SQLiteOpenHelper抽象类,是一个辅助器类,需要 一个构造函数和重写两个方法。
- *
- */
- public class MySQLiteOpenHelper extends SQLiteOpenHelper {
- public static final String DATABASE_NAME = "text.db"; // 数据库名
- public static final int VERSION = 1; // 版本号
- public static final String TABLE_NAME = "text"; // 表名
- public static final String ID = "id";
- public static final String IMAGE = "image";
-
- public MySQLiteOpenHelper(Context context) {
- super(context, DATABASE_NAME, null, VERSION);
- }
-
- /**
- * 在数据库第一次生成的时候会调用这个方法,同时我们在这个方法里边生成数据库表
- */
- @Override
- public void onCreate(SQLiteDatabase db) {
- // 创建数据表的操作
- String strSQL = "CREATE TABLE " + TABLE_NAME + "(" + ID
- + " INTEGER PRIMARY KEY AUTOINCREMENT," + IMAGE + " blob not null );";
-
- db.execSQL(strSQL);
- }
-
- /**
- * 更新或者升级数据库的时候会自动调用这个方法,一般我们会在这个方法中 删除数据表,然后再创建新的数据表操作。
- */
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- Log.e("AndyDemo", "onUpgrade");
- }
- }
Activity:
- private Button btn_newTable, btn_addOne, get_Image;
- private TextView tv;
- private ImageView showimage;
- private MySQLiteOpenHelper myOpenHelper;
- private SQLiteDatabase sqlitedb;
- private File path = new File("sdcard/text"); // 数据库文件目录
- private File f = new File("sdcard/text/text.db"); // 数据库文件
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- init();
-
- // 实例化默认数据库辅助操作对象
- myOpenHelper = new MySQLiteOpenHelper(this);
-
- // SD卡中创建数据库文件
- if (!path.exists()) { // 判断目录是否存在
- path.mkdirs(); // 创建目录
- }
- if (!f.exists()) { // 判断文件是否存在
- try {
- f.createNewFile(); // 创建文件
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- /**
- * 初始化UI界面
- */
- private void init() {
- tv = (TextView) findViewById(R.id.tv_result);
- btn_newTable = (Button) findViewById(R.id.newTable);
- btn_addOne = (Button) findViewById(R.id.addOne);
- get_Image = (Button) findViewById(R.id.getimage);
- showimage = (ImageView) findViewById(R.id.showimage);
- btn_newTable.setOnClickListener(new ClickEvent());
- btn_addOne.setOnClickListener(new ClickEvent());
- get_Image.setOnClickListener(new ClickEvent());
-
- }
-
- class ClickEvent implements OnClickListener {
- @Override
- public void onClick(View v) {
- try {
- // SD卡中创建数据库,实例化sqlitedb的操作如下
- sqlitedb = SQLiteDatabase.openOrCreateDatabase(f, null);
- if (v == btn_newTable) { // 1.新建数据表
- String TABLE_NAME = "text";
- String ID = "id";
- String IMAGE = "image";
- String str_sql2 = "CREATE TABLE " + TABLE_NAME + "(" + ID
- + " INTEGER PRIMARY KEY AUTOINCREMENT," + IMAGE
- + " blob not null );";
- sqlitedb.execSQL(str_sql2);
- tv.setText("新建数据表成功!");
-
- } else if (v == btn_addOne) { // 2.插入一条记录
- ContentValues values = new ContentValues();
- values.put(
- MySQLiteOpenHelper.IMAGE,
- drawableChange(getResources().getDrawable(
- R.drawable.ic_launcher)));
- sqlitedb.insert(MySQLiteOpenHelper.TABLE_NAME, null, values);
- tv.setText("添加新数据成功!");
- } else if (v == get_Image) {
- Cursor c = sqlitedb.rawQuery("select * from text", null);
- c.moveToLast();
-
- if (c.isLast()) {
- byte[] blob = c.getBlob(c
- .getColumnIndex(MySQLiteOpenHelper.IMAGE));
- Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0,
- blob.length);
- showimage.setImageBitmap(bmp);
- }
- c.close();
- }
- } catch (Exception e) {
- tv.setText("操作失败");
- } finally {
- sqlitedb.close();
- }
- }
- }
-
- /**
- * drawable转化成字节数组
- *
- * @param drawable
- * @return
- */
- private byte[] drawableChange(Drawable drawable) {
-
- Bitmap bm = ((BitmapDrawable) drawable).getBitmap();
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
- byte[] date = baos.toByteArray();
- return date;
- }
新建一张表,插入一张图片,效果图如下:
查看表中的数据如下:
取出图片:
OK!!搞定! 最后别忘了加权限:
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。