dimanche 6 décembre 2015

Unable to retrieve and set image from sqlite to ImageView in android

I am trying to save an Image inside the Sqlite database in Android.I want to set that image on an ImageView after inserting it. I tried with the following code but the image does not set. Can anyone tell me the problem with the code.

   Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        Log.i("ic launcher byte array", ""+byteArray);
        db.insert_signature_table(String.valueOf(AssignedJobs.jobId), String.valueOf(AssignedJobs.teamId), byteArray);
        Log.i("get all signature data", ""+db.getAll_signature_table().get(0).get("sign"));
         byte[] bb = db.getAll_signature_table().get(0).get("sign").getBytes();
         Log.i("decodes", ""+bb);
         Bitmap bmp1 = BitmapFactory.decodeByteArray(bb, 0, bb.length);
         edt_signature.setImageBitmap(bmp1);

dbHelper class

  query_signature_table = "CREATE TABLE signature_table(JobId STRING,TeamId STRING,sign BLOB)";
database.execSQL(query_signature_table);



public void insert_signature_table(String jobId, String teamId, byte[] imageInByte) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();

values.put("JobId", jobId);
values.put("TeamId", teamId);
values.put("sign", imageInByte);

Log.i("values assigned", "" + imageInByte);

database.insert("signature_table", null, values);
database.close();
}


public ArrayList<HashMap<String, String>> getAll_signature_table() {
ArrayList<HashMap<String, String>> wordList;
wordList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT  * FROM signature_table";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
    do {
        HashMap<String, String> map = new HashMap<String, String>();

        map.put("JobId", cursor.getString(0));
        map.put("RigId", cursor.getString(1));

          byte[] temp_image = cursor.getBlob(2);

          map.put("sign",""+temp_image);

        wordList.add(map);
    } while (cursor.moveToNext());
}

Aucun commentaire:

Enregistrer un commentaire