Android數(shù)據(jù)庫SD卡創(chuàng)建和圖片存取操作
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òu)造函數(shù)和重寫兩個方法。
*
*/
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "text.db"; // 數(shù)據(jù)庫名
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);
}
/**
* 在數(shù)據(jù)庫第一次生成的時候會調(diào)用這個方法,同時我們在這個方法里邊生成數(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);
}
/**
* 更新或者升級數(shù)據(jù)庫的時候會自動調(diào)用這個方法,一般我們會在這個方法中 刪除數(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();
// 實例化默認(rèn)數(shù)據(jù)庫輔助操作對象
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ù)庫,實例化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"/>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android創(chuàng)建和使用數(shù)據(jù)庫SQLIte
- Android實現(xiàn)創(chuàng)建或升級數(shù)據(jù)庫時執(zhí)行語句
- android創(chuàng)建數(shù)據(jù)庫(SQLite)保存圖片示例
- Android SQLite數(shù)據(jù)庫增刪改查操作的使用詳解
- Android使用SQLite數(shù)據(jù)庫的簡單實例
- Android中操作SQLite數(shù)據(jù)庫快速入門教程
- Android實現(xiàn)將已發(fā)送的短信寫入短信數(shù)據(jù)庫的方法
- Android SQLite數(shù)據(jù)庫增刪改查操作的案例分析
- 實例講解Android App使用自帶的SQLite數(shù)據(jù)庫的基本方法
- Android操作SQLite數(shù)據(jù)庫(增、刪、改、查、分頁等)及ListView顯示數(shù)據(jù)的方法詳解
- Android編程之?dāng)?shù)據(jù)庫的創(chuàng)建方法詳解
相關(guān)文章
Android?Banner本地和網(wǎng)絡(luò)輪播圖使用介紹
大家好,本篇文章講的是Android?Banner本地和網(wǎng)絡(luò)輪播圖使用介紹,感興趣的同學(xué)趕快來看一看吧,希望本篇文章對你起到幫助2021-11-11
Android 重寫ViewGroup 分析onMeasure()和onLayout()方法
這篇文章主要介紹了Android 重寫ViewGroup 分析onMeasure()和onLayout()方法的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android編程UI設(shè)計之GridView和ImageView的用法
這篇文章主要介紹了Android編程UI設(shè)計之GridView和ImageView的用法,結(jié)合實例形式較為詳細(xì)的分析了Android中GridView和ImageView組件的相關(guān)方法使用技巧,需要的朋友可以參考下2016-01-01
Android中的Shape和Selector的結(jié)合使用實例
這篇文章主要介紹了Android中的Shape和Selector的結(jié)合使用實例,本文直接給出實例代碼,需要的朋友可以參考下2015-06-06
Android Studio一直處于Building的兩種解決方法
很多朋友都遇到過打開別人的項目一直處于Building‘XXX’Gradle project info的情況。下面小編給大家?guī)砹薃ndroid Studio一直處于Building的解決方法,感興趣的朋友一起看看吧2018-08-08
Android開發(fā)四大組件之實現(xiàn)電話攔截和電話錄音
這篇文章給大家介紹Android開發(fā)四大組件之實現(xiàn)電話攔截和電話錄音,涉及到android四大基本組件在程序中的應(yīng)用,對android四大基本組件感興趣的朋友可以參考下本篇文章2015-10-10
android完美實現(xiàn) 拍照 選擇圖片 剪裁等代碼分享
本文給大家分享了2個安卓實現(xiàn)實現(xiàn) 拍照 選擇圖片 剪裁等的代碼,都是從正式項目中提取出來了,非常實用,有需要的小伙伴可以參考下。2016-01-01
Flutter實現(xiàn)倒計時秒數(shù)轉(zhuǎn)時分秒然后倒計時功能
有一個需求,需要在頁面進(jìn)行顯示倒計時,倒計時結(jié)束后,做相應(yīng)的邏輯處理,這篇文章主要介紹了Flutter實現(xiàn)倒計時功能,秒數(shù)轉(zhuǎn)時分秒,然后倒計時,需要的朋友可以參考下2023-08-08
Android開發(fā)實現(xiàn)廣告無限循環(huán)功能示例
這篇文章主要介紹了Android開發(fā)實現(xiàn)廣告無限循環(huán)功能,結(jié)合完整實例形式分析了Android廣告圖片輪播功能的具體實現(xiàn)步驟與相關(guān)功能、布局等操作技巧,需要的朋友可以參考下2017-11-11

