您好,登錄后才能下訂單哦!
Android數(shù)據(jù)庫中的創(chuàng)建,圖片的存、取操作如下:
數(shù)據(jù)庫類:
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; /** * 此類繼承了SQLiteOpenHelper抽象類,是一個(gè)輔助器類,需要 一個(gè)構(gòu)造函數(shù)和重寫兩個(gè)方法。 * */ public class MySQLiteOpenHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME = "text.db"; // 數(shù)據(jù)庫名 public static final int VERSION = 1; // 版本號(hào) 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); } /** * 在數(shù)據(jù)庫第一次生成的時(shí)候會(huì)調(diào)用這個(gè)方法,同時(shí)我們?cè)谶@個(gè)方法里邊生成數(shù)據(jù)庫表 */ @Override public void onCreate(SQLiteDatabase db) { // 創(chuàng)建數(shù)據(jù)表的操作 String strSQL = "CREATE TABLE " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + IMAGE + " blob not null );"; db.execSQL(strSQL); } /** * 更新或者升級(jí)數(shù)據(jù)庫的時(shí)候會(huì)自動(dòng)調(diào)用這個(gè)方法,一般我們會(huì)在這個(gè)方法中 刪除數(shù)據(jù)表,然后再創(chuàng)建新的數(shù)據(jù)表操作。 */ @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"); // 數(shù)據(jù)庫文件目錄 private File f = new File("sdcard/text/text.db"); // 數(shù)據(jù)庫文件 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); // 實(shí)例化默認(rèn)數(shù)據(jù)庫輔助操作對(duì)象 myOpenHelper = new MySQLiteOpenHelper(this); // SD卡中創(chuàng)建數(shù)據(jù)庫文件 if (!path.exists()) { // 判斷目錄是否存在 path.mkdirs(); // 創(chuàng)建目錄 } if (!f.exists()) { // 判斷文件是否存在 try { f.createNewFile(); // 創(chuàng)建文件 } 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卡中創(chuàng)建數(shù)據(jù)庫,實(shí)例化sqlitedb的操作如下 sqlitedb = SQLiteDatabase.openOrCreateDatabase(f, null); if (v == btn_newTable) { // 1.新建數(shù)據(jù)表 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("新建數(shù)據(jù)表成功!"); } 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("添加新數(shù)據(jù)成功!"); } 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轉(zhuǎn)化成字節(jié)數(shù)組 * * @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; }
新建一張表,插入一張圖片,效果圖如下:
查看表中的數(shù)據(jù)如下:
取出圖片:
OK!!搞定! 最后別忘了加權(quán)限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。