Android sqlite 存取图片完整示例
时间: 2023-08-05 21:44:22 浏览: 122
android sqlite 示例
以下是一个完整的示例,演示如何使用SQLite存储和读取图片:
1. 创建数据库表
在创建SQLite数据库表时,需要添加一个BLOB类型的列,用于存储图片的二进制数据。示例代码如下:
```java
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "my_database";
private static final int DB_VERSION = 1;
private static final String TABLE_NAME = "image_table";
private static final String ID_COLUMN = "id";
private static final String NAME_COLUMN = "name";
private static final String IMAGE_COLUMN = "image";
public MyDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABLE_NAME + " (" +
ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
NAME_COLUMN + " TEXT, " +
IMAGE_COLUMN + " BLOB)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// do nothing
}
}
```
2. 存储图片
在存储图片时,需要将图片转换成二进制数据,并将其保存到数据库中。示例代码如下:
```java
public void saveImageToDB(String name, byte[] image) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(NAME_COLUMN, name);
cv.put(IMAGE_COLUMN, image);
db.insert(TABLE_NAME, null, cv);
db.close();
}
```
其中,`name`是图片的名称,`image`是图片的二进制数据。
3. 读取图片
在读取图片时,需要从数据库中查询指定名称的图片,并将其转换成Bitmap对象。示例代码如下:
```java
public Bitmap getImageFromDB(String name) {
SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {IMAGE_COLUMN};
String selection = NAME_COLUMN + "=?";
String[] selectionArgs = {name};
Cursor cursor = db.query(TABLE_NAME, columns, selection, selectionArgs, null, null, null);
if (cursor.moveToFirst()) {
byte[] image = cursor.getBlob(cursor.getColumnIndex(IMAGE_COLUMN));
cursor.close();
db.close();
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
cursor.close();
db.close();
return null;
}
```
其中,`name`是要读取的图片的名称,返回值是一个Bitmap对象。
4. 完整示例
以下是一个完整的示例,演示如何使用SQLite存储和读取图片:
```java
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 1;
private ImageView imageView;
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MyDatabaseHelper(this);
imageView = findViewById(R.id.image_view);
Button selectButton = findViewById(R.id.select_button);
selectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_CODE);
}
});
Button saveButton = findViewById(R.id.save_button);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
byte[] image = getBitmapAsByteArray(bitmap);
dbHelper.saveImageToDB("my_image", image);
Toast.makeText(MainActivity.this, "Image saved to database", Toast.LENGTH_SHORT).show();
}
});
Button loadButton = findViewById(R.id.load_button);
loadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = dbHelper.getImageFromDB("my_image");
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
Toast.makeText(MainActivity.this, "Image not found in database", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
Uri uri = data.getData();
imageView.setImageURI(uri);
}
}
private byte[] getBitmapAsByteArray(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
return outputStream.toByteArray();
}
@Override
protected void onDestroy() {
super.onDestroy();
dbHelper.close();
}
}
```
以上示例代码仅供参考,实际使用时需要进行适当修改。另外,存储图片到SQLite中并不是一个好的做法,建议存储图片到文件系统中,然后在数据库中存储图片的路径。
阅读全文