涉及要点:
1 SQLite数据库的操作
2 图片的存储与读出(转为byte格式)
1 SQLite数据库的操作
2 图片的存储与读出(转为byte格式)
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/ID_SaveButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="@string/save_button1" /> <Button android:id="@+id/ID_SaveButton2" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/ID_SaveButton1" android:layout_alignTop="@+id/ID_SaveButton1" android:text="@string/save_button2" /> <Button android:id="@+id/ID_QueryButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/ID_SaveButton1" android:text="@string/query_button1" /> <Button android:id="@+id/ID_QueryButton2" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/ID_QueryButton1" android:layout_alignTop="@+id/ID_QueryButton1" android:text="@string/query_button2" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/ID_QueryButton1" android:layout_below="@+id/ID_QueryButton1" android:contentDescription="@string/image1" android:src="@drawable/picture2" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/ID_QueryButton2" android:layout_below="@+id/ID_QueryButton2" android:contentDescription="@string/image2" android:src="@drawable/picture3" /> </RelativeLayout>
/* 数据库类 */ package com.example.testpackage1; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; public class SQLLiteOpenHelper extends SQLiteOpenHelper { public SQLLiteOpenHelper(Context context, String name,CursorFactory factory, int version) { super(context, name, factory, version); } public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE imagetable (_id INTEGER PRIMARY KEY AUTOINCREMENT,image BLOB)"); } public void onUpgrade(SQLiteDatabase db,int nOldVersion,int nNewVersion) { } } /* MainActivity 类 */ package com.example.testpackage1; import java.io.ByteArrayOutputStream; import java.io.IOException; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class MainActivity extends Activity { protected Button btSave1,btSave2,btQuery1,btQuery2; private final int CLICK_SAVE1 = 1; private final int CLICK_SAVE2 = 2; private final int CLICK_QUERY1 = 11; private final int CLICK_QUERY2 = 12; private final int ID_SAVE1 = 1; private final int ID_SAVE2 = 2; private ImageView m_ImageView1,m_ImageView2; private SQLLiteOpenHelper m_tSQLLiteOpenHelper; private SQLiteDatabase m_tDB; private String TABLE_NAME = "imagetable"; private String QUERY_ID = "_id"; private String QUERY_IMAGE = "image"; private String DB_Name = "saveimage1.db"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BindButtonListenerWithId(btSave1,R.id.ID_SaveButton1,CLICK_SAVE1); BindButtonListenerWithId(btSave2,R.id.ID_SaveButton2,CLICK_SAVE2); BindButtonListenerWithId(btQuery1,R.id.ID_QueryButton1,CLICK_QUERY1); BindButtonListenerWithId(btQuery2,R.id.ID_QueryButton2,CLICK_QUERY2); //null采用默认工厂类 m_tSQLLiteOpenHelper = new SQLLiteOpenHelper(this,DB_Name,null,1); m_tDB = m_tSQLLiteOpenHelper.getReadableDatabase(); m_ImageView1 = (ImageView)findViewById(R.id.imageView1); m_ImageView2 = (ImageView)findViewById(R.id.imageView2); } private void BindButtonListenerWithId(Button button,int nID,int nTagID) { button = (Button)findViewById(nID); button.setOnClickListener(new ClickButtonListener()); button.setTag(nTagID); } private final class ClickButtonListener implements View.OnClickListener { @Override public void onClick(View view) { switch((Integer) view.getTag()) { case CLICK_SAVE1: {//根据默认的方式,自动增长 try { //将图片转为byte数据 Log.w("将图片转为byte数据", "CLICK_SAVE1"); Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.picture2); int nSize = bitmap1.getWidth()*bitmap1.getHeight()*4; ByteArrayOutputStream baos = new ByteArrayOutputStream(nSize); bitmap1.compress(Bitmap.CompressFormat.PNG, 100, baos); byte [] imageBytes = baos.toByteArray(); //数据存入容器 Log.w("数据存入容器", "CLICK_SAVE1"); ContentValues content = new ContentValues(); content.put(QUERY_ID, ID_SAVE1); content.put(QUERY_IMAGE, imageBytes); Log.w("存入数据库 ", "CLICK_SAVE1"); m_tDB.insert(TABLE_NAME, null, content); Log.w("baos.close();", "CLICK_SAVE1"); baos.close(); }catch (IOException e) { Log.w("e.printStackTrace();", "CLICK_SAVE1"); e.printStackTrace(); } }break; case CLICK_SAVE2: { try { //将图片转为byte数据 Log.w("将图片转为byte数据", "CLICK_SAVE2"); Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.picture3); int nSize = bitmap2.getWidth()*bitmap2.getHeight()*4; ByteArrayOutputStream baos = new ByteArrayOutputStream(nSize); bitmap2.compress(Bitmap.CompressFormat.PNG, 100, baos); byte [] imageBytes = baos.toByteArray(); //数据存入容器 Log.w("数据存入容器", "CLICK_SAVE2"); ContentValues content = new ContentValues(); content.put(QUERY_ID, ID_SAVE2); content.put(QUERY_IMAGE, imageBytes); Log.w("存入数据库 ", "CLICK_SAVE2"); m_tDB.insert(TABLE_NAME, null, content); baos.close(); }catch (IOException e) { e.printStackTrace(); } }break; case CLICK_QUERY1: { Cursor sor = m_tDB.query(TABLE_NAME, new String[] {QUERY_ID,QUERY_IMAGE},null,null, null, null, null); byte [] imageDatas = null; if(sor.moveToNext()) { Log.w("取得数据并显示 ", "CLICK_QUERY1"); imageDatas = sor.getBlob(sor.getColumnIndex(QUERY_IMAGE)); Bitmap imageBitmap = BitmapFactory.decodeByteArray(imageDatas, 0, imageDatas.length); m_ImageView2.setImageBitmap(imageBitmap); } }break; case CLICK_QUERY2: { Cursor sor = m_tDB.query(TABLE_NAME, new String[] {QUERY_ID,QUERY_IMAGE},null,null, null, null, null); byte [] imageDatas = null; if(sor.moveToNext()) { Log.w("取得数据并显示 ", "CLICK_QUERY2"); imageDatas = sor.getBlob(sor.getColumnIndex(QUERY_IMAGE)); Bitmap imageBitmap = BitmapFactory.decodeByteArray(imageDatas, 0, imageDatas.length); m_ImageView1.setImageBitmap(imageBitmap); } }break; } } } public void onDestroy(){ super.onDestroy(); //退出程序时,必须关闭数据库。 m_tDB.close(); this.deleteDatabase(DB_Name); } }