为了将大量数据(如图像)插入数据库,可以使用 SQLite,将我们的图像转换为字节数组并存储到 SQLite。
在这里,我们看到一个示例,即存储员工详细信息,例如
员工姓名
员工照片
员工年龄
对于员工姓名,我们使用文本数据类型,对于员工年龄整数数据类型。但是要存储员工照片,使用哪种数据类型?当时我们使用 BLOB 数据类型。
insertEmpDetails()
该方法以员工类为参数,并将所有员工信息存储到员工表中。在这里我们可以看到在将图像插入数据库之前,我们应该将您的位图图像转换为字节数组,因为我们正在使用实用程序类 getBytes() 方法。
retriveEmpDetails()
从数据库中检索员工详细信息并返回员工对象的方法。要获取所有姓名、照片、年龄,我们使用相应的 getXXX() 方法。在检索照片时,我们将得到图像的 blob[] 数组。此时我们需要将字节数组转换为图像。为此,我们使用 Utility 类中的 getPhoto() 方法,它将我们的 blob 数组转换为位图图像和重新运行位图图像。
UI 布局(activity_main.xml)
此布局用于查看
员工姓名
员工照片
员工年龄
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EMPLOYEE_NAME" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:text="NAME" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EMPLOYEE_PHOTO" />
<ImageView
android:id="@+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EMPLOYEE_AGE" />
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:text="AGE" />
</LinearLayout>
</LinearLayout>
Android Activity(InsertandRetriveBlobData.java)
package com.androidsurya.sqliteexample;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class InsertandRetriveBlobData extends Activity {
private DBhelper DbHelper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DbHelper = new DBhelper(this);
Employee employee_One = new Employee(BitmapFactory.decodeResource(
getResources(), R.drawable.photo), "Surya", 25);
DbHelper.open();
DbHelper.insertEmpDetails(employee_One);
DbHelper.close();
employee_One = null;
DbHelper.open();
employee_One = DbHelper.retriveEmpDetails();
DbHelper.close();
TextView empname = (TextView) findViewById(R.id.name);
empname.setText(employee_One.getName());
ImageView empphoto = (ImageView) findViewById(R.id.photo);
empphoto.setImageBitmap(employee_One.getBitmap());
TextView empage = (TextView) findViewById(R.id.age);
empage.setText("" + employee_One.getAge());
}
}
DBhelper.java
这是我们在这里声明的数据库类
insertEmpDetails()
插入员工详细信息的
retriveEmpDetails()
方法和检索员工详细信息的方法
package com.androidsurya.sqliteexample;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBhelper {
public static final String EMP_ID = "id";
public static final String EMP_NAME = "name";
public static final String EMP_AGE = "age";
public static final String EMP_PHOTO = "photo";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "EmployessDB.db";
private static final int DATABASE_VERSION = 1;
private static final String EMPLOYEES_TABLE = "Employees";
private static final String CREATE_EMPLOYEES_TABLE = "create table "
+ EMPLOYEES_TABLE + " (" + EMP_ID
+ " integer primary key autoincrement, " + EMP_PHOTO
+ " blob not null, " + EMP_NAME + " text not null unique, "
+ EMP_AGE + " integer );";
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_EMPLOYEES_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + EMPLOYEES_TABLE);
onCreate(db);
}
}
public void Reset() {
mDbHelper.onUpgrade(this.mDb, 1, 1);
}
public DBhelper(Context ctx) {
mCtx = ctx;
mDbHelper = new DatabaseHelper(mCtx);
}
public DBhelper open() throws SQLException {
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public void insertEmpDetails(Employee employee) {
ContentValues cv = new ContentValues();
cv.put(EMP_PHOTO, Utility.getBytes(employee.getBitmap()));
cv.put(EMP_NAME, employee.getName());
cv.put(EMP_AGE, employee.getAge());
mDb.insert(EMPLOYEES_TABLE, null, cv);
}
public Employee retriveEmpDetails() throws SQLException {
Cursor cur = mDb.query(true, EMPLOYEES_TABLE, new String[] { EMP_PHOTO,
EMP_NAME, EMP_AGE }, null, null, null, null, null, null);
if (cur.moveToFirst()) {
byte[] blob = cur.getBlob(cur.getColumnIndex(EMP_PHOTO));
String name = cur.getString(cur.getColumnIndex(EMP_NAME));
int age = cur.getInt(cur.getColumnIndex(EMP_AGE));
cur.close();
return new Employee(Utility.getPhoto(blob), name, age);
}
cur.close();
return null;
}
}
实用程序.java
实用程序类是这里的主要角色在从 SQLite 数据库插入图像和检索图像之前,我们使用以下这些方法。
getBytes()
将位图图像转换为字节数组并重新运行 byte[]
getPhoto()
将字节[] 转换为位图并重新运行位图图像
package com.androidsurya.sqliteexample;
import java.io.ByteArrayOutputStream;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
public class Utility {
// convert from bitmap to byte array
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}
// convert from byte array to bitmap
public static Bitmap getPhoto(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
}
在 AndroidManifest 文件中注册 Android Activity
<activity
android:name="com.androidsurya.sqliteexample.InsertandRetriveBlobData"
android:label="@string/app_name" >