将图像保存到SQLlite数据库

时间:2017-06-13 08:05:56

标签: java android sqlite

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

我正在创建一个应用程序,客户端将在该设备上签名然后将表单提交到电子邮件地址。表单正常工作,数据正在正确提交,但我在提交表单之前无法获取签名。

所以这就是我到目前为止所做的  对于我的ExampleDBHelper.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"让客户登录。

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

谢谢大家: - )

1 个答案:

答案 0 :(得分:0)

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

修改

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();
}