mardi 29 septembre 2015

How to retrieved image from sqlite to list view

My code here EmployeeAppActivity

My code working well in list view without image. i need for employee photo in list view, i getting error for when call image from database to list view please help me.

// Primitive Variables
String selected_ID = "";

// Widget GUI Declare
EditText txtEname, txtDesig, txtSalary;
Button btnAddEmployee, btnUpdate, btnDelete;
ListView lvEmployees;

// DB Objects
DBHelper helper;
SQLiteDatabase db;

// Adapter Object
SimpleCursorAdapter adapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Init DB Objects
    helper = new DBHelper(this);

    // Widget GUI Init
    txtEname = (EditText) findViewById(R.id.txtEname);
    txtDesig = (EditText) findViewById(R.id.txtDesig);
    txtSalary = (EditText) findViewById(R.id.txtSalary);
    lvEmployees = (ListView) findViewById(R.id.lvEmployees);

    btnAddEmployee = (Button) findViewById(R.id.btnAdd);
    btnUpdate = (Button) findViewById(R.id.btnUpdate);
    btnDelete = (Button) findViewById(R.id.btnDelete);

    // Attached Listener
    btnAddEmployee.setOnClickListener(this);
    btnUpdate.setOnClickListener(this);
    btnDelete.setOnClickListener(this);
    lvEmployees.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View v,
                int position, long id) {

            String name, desig, salary;
            byte[] empphoto;

            // Display Selected Row of Listview into EditText widget

            Cursor row = (Cursor) adapter.getItemAtPosition(position);
            selected_ID = row.getString(0);
            name = row.getString(1);
            desig = row.getString(2);
            salary = row.getString(3);
            empphoto=row.getBlob(4);

            txtEname.setText(name);
            txtDesig.setText(desig);
            txtSalary.setText(salary);
        }
    });

    // Fetch Data from database
    fetchData();
}

@Override
public void onClick(View v) {

    // Perform CRUD Operation

    if (v == btnAddEmployee) {

        // Add Record with help of ContentValues and DBHelper class object

        Bitmap b=BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        //Bitmap b=BitmapFactory.decodeFile(imagepath);
        //ContentValues cv=new ContentValues();
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        b.compress(Bitmap.CompressFormat.JPEG, 90, bos);
        byte[] photo=bos.toByteArray();
        ContentValues values = new ContentValues();
        values.put(DBHelper.C_ENAME, txtEname.getText().toString());
        values.put(DBHelper.C_DESIGNATION, txtDesig.getText().toString());
        values.put(DBHelper.C_SALARY, txtSalary.getText().toString());
        values.put(DBHelper.C_Dp, photo);

        // Call insert method of SQLiteDatabase Class and close after
        // performing task
        db = helper.getWritableDatabase();
        db.insert(DBHelper.TABLE, null, values);
        db.close();

        clearFields();
        Toast.makeText(this, "Employee Added Successfully",
                Toast.LENGTH_LONG).show();

        // Fetch Data from database and display into listview
        fetchData();

    }
    if (v == btnUpdate) {

        // Update Record with help of ContentValues and DBHelper class
        // object

        ContentValues values = new ContentValues();
        values.put(DBHelper.C_ENAME, txtEname.getText().toString());
        values.put(DBHelper.C_DESIGNATION, txtDesig.getText().toString());
        values.put(DBHelper.C_SALARY, txtSalary.getText().toString());

        // Call update method of SQLiteDatabase Class and close after
        // performing task
        db = helper.getWritableDatabase();
        db.update(DBHelper.TABLE, values, DBHelper.C_ID + "=?",
                new String[] { selected_ID });
        db.close();

        // Fetch Data from database and display into listview
        fetchData();
        Toast.makeText(this, "Record Updated Successfully",
                Toast.LENGTH_LONG).show();
        clearFields();

    }
    if (v == btnDelete) {

        // Call delete method of SQLiteDatabase Class to delete record and
        // close after performing task
        db = helper.getWritableDatabase();
        db.delete(DBHelper.TABLE, DBHelper.C_ID + "=?",
                new String[] { selected_ID });
        db.close();

        // Fetch Data from database and display into listview
        fetchData();
        Toast.makeText(this, "Record Deleted Successfully",
                Toast.LENGTH_LONG).show();
        clearFields();

    }

}

// Clear Fields
private void clearFields() {
    txtEname.setText("");
    txtDesig.setText("");
    txtSalary.setText("");
}

// Fetch Fresh data from database and display into listview
private void fetchData() {

    db = helper.getReadableDatabase();
    Cursor c = db.query(DBHelper.TABLE, null, null, null, null, null, null);
    adapter = new SimpleCursorAdapter(
            this,
            R.layout.row,
            c,
            new String[] { DBHelper.C_ENAME, DBHelper.C_SALARY, DBHelper.C_DESIGNATION ,DBHelper.C_Dp},

            new int[] { R.id.lblEname, R.id.lblSalary, R.id.lblDesignation,R.id.empdp });
    lvEmployees.setAdapter(adapter);
}

DBHelper class here

static final String DATABASE = "empapp.db";
static final int VERSION = 1;
static final String TABLE = "emp";

static final String C_ID = "_id";
static final String C_ENAME = "ename";
static final String C_DESIGNATION = "designation";
static final String C_SALARY = "salary";
static final String C_Dp = "photo";

// Override constructor
public DBHelper(Context context) {
    super(context, DATABASE, null, VERSION);

}

// Override onCreate method
@Override
public void onCreate(SQLiteDatabase db) {

    // Create Employee table with following fields
    // _ID, ENAME, DESIGNATION and SALARY
    db.execSQL("CREATE TABLE " + TABLE + " ( " + C_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + C_ENAME + " text, "
            + C_DESIGNATION + " text, " + C_SALARY + " text, " + C_Dp+ " BLOB )");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    // Drop old version table
    db.execSQL("Drop table " + TABLE);

    // Create New Version table
    onCreate(db);
}

Logcat error

09-30 03:45:38.139: E/AndroidRuntime(30628): FATAL EXCEPTION: main

09-30 03:45:38.139: E/AndroidRuntime(30628): android.database.sqlite.SQLiteException: unknown error (code 0): Unable to convert BLOB to string 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.database.CursorWindow.nativeGetString(Native Method) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.database.CursorWindow.getString(CursorWindow.java:434) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:150) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.widget.CursorAdapter.getView(CursorAdapter.java:250) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.widget.AbsListView.obtainView(AbsListView.java:2159) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.widget.ListView.makeAndAddView(ListView.java:1831) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.widget.ListView.fillDown(ListView.java:674) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.widget.ListView.fillFromTop(ListView.java:735) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.widget.ListView.layoutChildren(ListView.java:1652) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.widget.AbsListView.onLayout(AbsListView.java:1994) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.View.layout(View.java:14003) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.ViewGroup.layout(ViewGroup.java:4375) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.View.layout(View.java:14003) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.ViewGroup.layout(ViewGroup.java:4375) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.View.layout(View.java:14003) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.ViewGroup.layout(ViewGroup.java:4375) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.View.layout(View.java:14003) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.ViewGroup.layout(ViewGroup.java:4375) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.View.layout(View.java:14003) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.ViewGroup.layout(ViewGroup.java:4375) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.Choreographer.doCallbacks(Choreographer.java:562) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.Choreographer.doFrame(Choreographer.java:532) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.os.Handler.handleCallback(Handler.java:725) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.os.Handler.dispatchMessage(Handler.java:92) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.os.Looper.loop(Looper.java:137) 09-30 03:45:38.139: E/AndroidRuntime(30628): at android.app.ActivityThread.main(ActivityThread.java:5039) 09-30 03:45:38.139: E/AndroidRuntime(30628): at java.lang.reflect.Method.invokeNative(Native Method) 09-30 03:45:38.139: E/AndroidRuntime(30628): at java.lang.reflect.Method.invoke(Method.java:511) 09-30 03:45:38.139: E/AndroidRuntime(30628): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 09-30 03:45:38.139: E/AndroidRuntime(30628): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 09-30 03:45:38.139: E/AndroidRuntime(30628): at dalvik.system.NativeStart.main(Native Method)

Aucun commentaire:

Enregistrer un commentaire