dimanche 13 décembre 2015

Unable to convert BLOB to string

In AddClaims, I save ListView (with data and image) into SQLite database.

AddClaims

 ArrayList<ImageAndText> images=new ArrayList<ImageAndText>();

  btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { //  button is clicked
                SB.insertStaffBenefit(images, a); // SB is the object of StaffAPI
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
            }
        });
    }

StaffAPI

 public long insertStaffBenefit(ArrayList<ImageAndText> imageText,long id)
    {
        id1=id;
        database=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        for(ImageAndText i:imageText) {
        String description=i.getDescription();
        Bitmap image=i.getImage();
        byte[]data=getBitmapAsByteArray(image);
        values.put(MydatabaseHelper.Description,description);
        values.put(MyDatabaseHelper.Image, data);
        database.insert(MyDatabaseHelper.TABLE_STAFF_BENEFIT, null, values);
        }
        database.close();
        return 0 ;
    }

public static byte[] getBitmapAsByteArray(Bitmap bitmap)
    {    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
        Log.e("TAG", "Blob size: " + outputStream.size() / 1024 + " KB");
        return outputStream.toByteArray();
    }

I not sure whether this is the correct way to save the image in listView, but it can display the Blob size. So I assume the image can be saved.

Next, I wanted to retrieve all the image and loaded to ListView in EditStaff

EditStaff

  public void BuildEditStaffList(long id)
    {
        Log.e("S", id+"");
        sqlcon.open();
        Cursor cursor1=sqlcon.readName(id);

        String[] columns=new String[]{
                MydatabaseHelper.image,MyDatabaseHelper.Description};

        int[] to=new int[]
                {
                        R.id.image,R.id.description};

        dataAdapter = new SimpleCursorAdapter(getActivity(), R.layout.retrieve_staff,
                cursor1,
                columns,
                to,
                0);

        listViewEdit.setAdapter(dataAdapter);
    }

StaffAPI

 public Cursor readName(long id)
    {
        Log.e("ID",id+"");
        Cursor c=database.rawQuery(" SELECT _id, Image, Description FROM " + MyDatabaseHelper.TABLE_STAFF_BENEFIT  + "  WHERE Ts_id = ?", new String[]{String.valueOf(id)}, null);

        if (c != null) {
            c.moveToFirst();
        }
        Log.e("ID",id+"");
        return c;
    }

The app crashed when I try to retrieve the image and text to ListView EditStaff.

 7602-7602/com.example.project.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.myapplication, PID: 7602
    android.database.sqlite.SQLiteException: unknown error (code 0): Unable to convert BLOB to string
            at android.database.CursorWindow.nativeGetString(Native Method)
            at android.database.CursorWindow.getString(CursorWindow.java:439)
            at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
            at android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:135)

After I remove the MydatabaseHelper.image and R.id.image in EditStaff, the text can be displayed on listView. So I guess the app crashed is because of the image. Is there any way to check whether the image is inserted correctly in SQLite? Thanks

I know this question has been asked before, but I don't know what should I change. Can someone please help me?

Aucun commentaire:

Enregistrer un commentaire