在我的 Android 应用程序中,用户可以插入他/她的数据(姓名、年龄等)以及作为个人资料图片的图像。插入数据后,用户单击保存按钮。然后插入的数据将保存在数据库中。数据库中的头像有blob类型。
我还需要从数据库中检索用户名和个人资料图片,并在表格布局中显示它们。
这是我将数据插入数据库的代码段。
case R.id.btnSave:
personName = etName.getText().toString();
date_of_birth = tvDOB.getText().toString();
age = tvAge.getText().toString();
int selected_rb_ID = genderGrp.getCheckedRadioButtonId();
RadioButton rb = (RadioButton) findViewById(selected_rb_ID);
gender = rb.getText().toString();
bloodGrp = spiBloodGrp.getSelectedItem().toString();
// get byte array from image view
Drawable d = image.getBackground();
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bitmap = bitDw.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
Person person = new Person();
person.setName(personName);
person.setDate_of_birth(date_of_birth);
person.setAge(age);
person.setGender(gender);
person.setBloodGrp(bloodGrp);
String proPic = new String(imageInByte);
person.setProfilePic(proPic);
ContentValues values = new ContentValues();
//values.put(COLUMN_PROFILE_PICTURE, person.getProfilePic().toString());
values.put(COLUMN_PROFILE_PICTURE, person.getProfilePic());
values.put(COLUMN_PERSON_NAME, person.getName());
values.put(COLUMN_DOB, person.getDate_of_birth());
values.put(COLUMN_AGE, person.getAge());
values.put(COLUMN_GENDER, person.getGender());
values.put(COLUMN_BLOODGRP, person.getBloodGrp());
DBHelper dbHelper = new DBHelper(this);
dbHelper.openDataBase();
if(dbHelper.insertIntoDatabase("EMPerson", values)){
dbHelper.onUpgrade(myDatabase, 1, 2);
Toast.makeText(getApplicationContext(), "Your Data has been saved successfully", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Oops ! Try again", Toast.LENGTH_SHORT).show();
}
dbHelper.closeDatabase();
这是我在表格布局中显示数据的代码段。
personList = helper.getPersonList();
loadTableLayouts();
这是我的 loadTableLayouts() 方法。
private void loadTableLayouts() {
int thheight = (int) (getResources().getDimension(R.dimen.cellLpH) / getResources()
.getDisplayMetrics().density);
int texthead = (int) (getResources().getDimension(R.dimen.boldtext) / getResources()
.getDisplayMetrics().density);
TableRow.LayoutParams rowLp = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT, thheight, 1.0f);
TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT, thheight, 1.0f);
TableRow th = new TableRow(this);
th.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.FILL_PARENT));
TextView thPersonName = new TextView(this);
thPersonName.setTextSize(texthead);
thPersonName.setTextColor(Color.RED);
thPersonName.setText("Name");
thPersonName.setTypeface(null, Typeface.BOLD);
thPersonName.setPadding(10, 0, 12, 0);
th.addView(thPersonName, cellLp);
TextView thProPic = new TextView(this);
thProPic.setTextSize(texthead);
thProPic.setTextColor(Color.RED);
thProPic.setText("Profile Picture");
thProPic.setTypeface(null, Typeface.BOLD);
thProPic.setPadding(10, 0, 12, 0);
th.addView(thProPic, cellLp);
tblPerson.addView(th, rowLp);
if (personList.size() > 0) {
for (int i = 0; i < personList.size(); i++) {
final TableRow tr = new TableRow(this);
tr.setTag(i);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.FILL_PARENT));
final TextView txtPersonName = new TextView(this);
txtPersonName.setTextSize(1, 12);
txtPersonName.setTextColor(Color.BLACK);
txtPersonName.setText("" + personList.get(i).getName());
txtPersonName.setPadding(10, 0, 12, 0);
tr.addView(txtPersonName, cellLp);
final ImageView imgPic = new ImageView(this);
imgPic.setImageBitmap(convertByteArrayToBitmap(personList
.get(i).getProfilePic().getBytes()));
imgPic.setPadding(10, 0, 12, 0);
tr.addView(imgPic, cellLp);
tr.setBackgroundDrawable(getResources().getDrawable(
R.drawable.table_row_selector));
tr.setVisibility(View.VISIBLE);
tblPerson.addView(tr, rowLp);
}
}
}
我使用 convertByteArrayToBitmap() 方法从字节数组中检索图像。
// Retrieve image from byte array:
public static Bitmap convertByteArrayToBitmap(
byte[] byteArrayToBeCOnvertedIntoBitMap) {
Bitmap bitMapImage = BitmapFactory.decodeByteArray(
byteArrayToBeCOnvertedIntoBitMap, 0,
byteArrayToBeCOnvertedIntoBitMap.length);
return bitMapImage;
}
这是我的 getPersonList() 方法。
public ArrayList<Person> getPersonList() {
ArrayList<Person> personList = new ArrayList<Person>();
String sql = "SELECT p.PersonName, p.ProfilePicture "
+ "FROM EMPerson p " + "ORDER BY p.PersonName";
System.out.println(sql);
ArrayList<?> stringList = selectRecordsFromDB(sql, null);
for (int i = 0; i < stringList.size(); i++) {
ArrayList<?> arrayList = (ArrayList<?>) stringList.get(i);
ArrayList<?> list = arrayList;
Person person = new Person();
person.setName((String) list.get(0));
person.setProfilePic((String) list.get(1));
personList.add(person);
}
return personList;
}
这是我的实体类代码段。
public class Person {
private String profilePic;
private String name, date_of_birth, age, gender, bloodGrp;
public String getProfilePic() {
return profilePic;
}
public void setProfilePic(String imageInByte) {
this.profilePic = imageInByte;
}
public String getName() {
return name;
}
.........
问题是这在运行时显示了表格布局,但它只显示了用户名。它没有显示个人资料图片。个人资料图片为空。即使,log cat 也没有显示任何错误消息。
如果有人能很好地解释这里发生了什么以及我该如何解决这个问题,我将非常感激。
干杯