将图像保存到 SQLlite 数据库后不显示图像



有人可以帮忙解决以下问题吗?

我正在创建一个应用程序,客户将在设备上登录,然后将表单提交到电子邮件地址。 表单有效且数据已正确提交,但我无法在提交表单之前最初显示签名。

这就是我目前所拥有的。
对于我的示例DBHelper.java类:

public static final String PERSON_FAULT_REPORTED = "faultreport";
public static final String PERSON_TECH_COMMENT = "techreport";
public static final String PERSON_SIGNATURE = "signature";
public static final String PERSON_JOB_NUMBER = "jobnumber";
public ExampleDBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(
            "CREATE TABLE " + PERSON_TABLE_NAME +
                    "(" + PERSON_COLUMN_ID + " INTEGER PRIMARY KEY, " +
                    PERSON_FAULT_REPORTED + " TEXT, " +
                    PERSON_TECH_COMMENT + " TEXT, " +
                    PERSON_SIGNATURE + " BLOB, " +
                    PERSON_JOB_NUMBER + " INTEGER)"
    );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + PERSON_TABLE_NAME);
    onCreate(db);
}
public boolean insertPerson(String faultreport,
                            String techreport,
                            String signature,
                            int jobnumber) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(PERSON_FAULT_REPORTED, faultreport);
    contentValues.put(PERSON_TECH_COMMENT, techreport);
    contentValues.put(PERSON_SIGNATURE, signature);
    contentValues.put(PERSON_JOB_NUMBER, jobnumber);
    db.insert(PERSON_TABLE_NAME, null, contentValues);
    return true;
}
public int numberOfRows() {
    SQLiteDatabase db = this.getReadableDatabase();
    int numRows = (int) DatabaseUtils.queryNumEntries(db, PERSON_TABLE_NAME);
    return numRows;
}
public boolean updatePerson(String faultreport,
                            String techreport,
                            String signature,
                            int jobnumber) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(PERSON_FAULT_REPORTED, faultreport);
    contentValues.put(PERSON_TECH_COMMENT, techreport);
    contentValues.put(PERSON_SIGNATURE, signature);

    contentValues.put(PERSON_JOB_NUMBER, jobnumber);
    db.update(PERSON_TABLE_NAME, contentValues, PERSON_COLUMN_ID + " = ? ", new String[]{Integer.toString(id)});
    return true;
}
public Integer deletePerson(Integer id) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(PERSON_TABLE_NAME,
            PERSON_COLUMN_ID + " = ? ",
            new String[]{Integer.toString(id)});
}
public Cursor getPerson(int id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("SELECT * FROM " + PERSON_TABLE_NAME + " WHERE " +
            PERSON_COLUMN_ID + "=?", new String[]{Integer.toString(id)});
    return res;
}
public Cursor getAllPersons() {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("SELECT * FROM " + PERSON_TABLE_NAME, null);
    return res;
}

然后在我的 MainActivity.java 类中:

    faultReported = (EditText) findViewById(R.id.EFaultReported);
    techComment = (EditText) findViewById(R.id.ETehComment);
    sigImage = (ImageView) findViewById(R.id.custimagesig);

    jobEditText = (EditText) findViewById(R.id.editCustComment);
    SimpleDateFormat calld = new SimpleDateFormat( "yyMMddHHmm" );
    jobEditText.setText( calld.format( new Date() ));
    saveButton = (Button) findViewById(R.id.saveButton);
    saveButton.setOnClickListener(this);
    buttonLayout = (LinearLayout) findViewById(R.id.buttonLayout);
    editButton = (Button) findViewById(R.id.editButton);
    editButton.setOnClickListener(this);
    deleteButton = (Button) findViewById(R.id.deleteButton);
    deleteButton.setOnClickListener(this);
    dbHelper = new ExampleDBHelper(this);
    if (personID > 0) {
        saveButton.setVisibility(View.GONE);
        buttonLayout.setVisibility(View.VISIBLE);
        Cursor rs = dbHelper.getPerson(personID);
        rs.moveToFirst();
        String faultrep = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_FAULT_REPORTED));
        String techcom = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_TECH_COMMENT));
        String custsign = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_SIGNATURE));

        int personAge = rs.getInt(rs.getColumnIndex(ExampleDBHelper.PERSON_JOB_NUMBER));
        if (!rs.isClosed()) {
            rs.close();
        }
        faultReported.setText((CharSequence) faultrep);
        faultReported.setFocusable(false);
        faultReported.setClickable(false);
        techComment.setText((CharSequence) techcom);
        techComment.setFocusable(false);
        techComment.setClickable(false);
        sigImage.setImageDrawable(Drawable.createFromPath(custsign));
        sigImage.setFocusable(false);
        sigImage.setClickable(false);
        jobEditText.setText((CharSequence) (personAge + ""));
        jobEditText.setFocusable(false);
        jobEditText.setClickable(false);
    }
}
@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.saveButton:
            persistPerson();
            return;
        case R.id.editButton:
            saveButton.setVisibility(View.VISIBLE);
            buttonLayout.setVisibility(View.GONE);
            faultReported.setEnabled(true);
            faultReported.setFocusableInTouchMode(true);
            faultReported.setClickable(true);
            techComment.setEnabled(true);
            techComment.setFocusableInTouchMode(true);
            techComment.setClickable(true);
            sigImage.setEnabled(true);
            sigImage.setFocusableInTouchMode(true);
            sigImage.setClickable(true);
            jobEditText.setEnabled(true);
            jobEditText.setFocusableInTouchMode(true);
            jobEditText.setClickable(true);
            return;
        case R.id.deleteButton:
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(R.string.deletePerson)
                    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dbHelper.deletePerson(personID);
                            Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(getApplicationContext(), FragmentJob.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                        }
                    })
                    .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // User cancelled the dialog
                        }
                    });
            AlertDialog d = builder.create();
            d.setTitle("Delete Form?");
            d.show();
            return;
    }
}
public void persistPerson() {
    if (personID > 0) {
        if (dbHelper.updatePerson(personID, 
                faultReported.getText().toString(),
                techComment.getText().toString(),
                sigImage.getDrawable().toString(),
                Integer.parseInt(jobEditText.getText().toString()))) {
            Toast.makeText(getApplicationContext(), "Form Update Successful", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(getApplicationContext(), FragmentJob.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        } else {
            Toast.makeText(getApplicationContext(), "Form Update Failed", Toast.LENGTH_SHORT).show();
        }
    } else {
        if (dbHelper.insertPerson(
                faultReported.getText().toString(),
                techComment.getText().toString(),
                sigImage.getDrawable().toString(),
                Integer.parseInt(jobEditText.getText().toString()))) {
            Toast.makeText(getApplicationContext(), "Form Inserted", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "Could not Insert Form", Toast.LENGTH_SHORT).show();
        }
        Intent intent = new Intent(getApplicationContext(), FragmentJob.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
}

最后是创建签名的方法:

 b1 = (Button) findViewById(R.id.SignatureButton);
    signImage = (ImageView) findViewById(R.id.custimagesig);
    b1.setOnClickListener(onButtonClick);
Button.OnClickListener onButtonClick = new Button.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent i = new Intent(CreateOrEditActivity.this, CaptureSignature.class);
        startActivityForResult(i, 0);
    }
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if (resultCode == 1) {
        Bitmap b = BitmapFactory.decodeByteArray(
                data.getByteArrayExtra("byteArray"), 0,
                data.getByteArrayExtra("byteArray").length);
        signImage.setImageBitmap(Bitmap.createScaledBitmap(b, 625, 625, false));
    }

不确定是否需要CaputureSignature.class? 这只是为了调出"Sinature Pad"供客户登录。

不确定我在这里做错了什么? 有人可以帮忙吗?如果您需要更多信息,请告诉我。

谢谢大家:-(

实际上您正在尝试获取图像字符串,但您将一个字节保存为 blob,因此您应该尝试在位图中byte[] custsign = rs.getBlob(rs.getColumnIndex(ExampleDBHelper.PERSON_SIGNATURE));并解析字节[]并设置为 imageView,而不是String custsign = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_SIGNATURE));

编辑

byte[] custsign = rs.getBlob(rs.getColumnIndex(ExampleDBHelper.PERSON_SIGNATURE));
if(custsign.length>0){
Bitmap bitmap = BitmapFactory.decodeByteArray(custsign, 0, custsign.length);
imageView.setImageBitmap(bitmap);
}else{
Toast.makeText(this,"Image is Not Fetching ",Toast.LENGTH_LONG).show();
}

最新更新