Android数据库中的创建,图片的存、取操作如下:
数据库类:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
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();
}
}
}
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();
}
}
}
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"/>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣的文章:Android创建和使用数据库SQLIteAndroid实现创建或升级数据库时执行语句android创建数据库(SQLite)保存图片示例Android SQLite数据库增删改查操作的使用详解Android使用SQLite数据库的简单实例Android中操作SQLite数据库快速入门教程Android实现将已发送的短信写入短信数据库的方法Android SQLite数据库增删改查操作的案例分析实例讲解Android App使用自带的SQLite数据库的基本方法Android操作SQLite数据库(增、删、改、查、分页等)及ListView显示数据的方法详解Android编程之数据库的创建方法详解