0

I'm trying to insert image to sqlite table.currently i have insert text value to sqlite i want to insert both text value and image to sqlite table.my app can capture image from camera and it's display in Imageview.when submit button click i want to insert that image to sqlite table.this is my code.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
mBut.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            submitData();

        }
    });

}

Show captured image

private void showPhoto(String photoUri) {
          File imageFile = new File (photoUri);
          if (imageFile.exists()){
             Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
             BitmapDrawable drawable = new BitmapDrawable(this.getResources(), bitmap);
             mCapturedImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
             mCapturedImage.setImageDrawable(drawable);
          }       
    }


private void submitData()
    {
        try {
        String VndCode=mValu.getText().toString();
        insertLC(VndCode);
        DbBackup.copyDatabase(getApplicationContext());
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    public void insertLC(String vendorcode) {
        // TODO Auto-generated method stub
        mDbA= new DBAdapter(this);
        mDbA.open();
        try {
            long i = mDbA.insertLC(vendorcode);
            if(i != -1){
        Toast.makeText(MainActivity.this, "Data Recorded",Toast.LENGTH_LONG).show();
            }
        } catch (SQLException e) {
            Toast.makeText(MainActivity.this, "Some problem occurred",
                    Toast.LENGTH_LONG).show();

        }
        mDbA.close();
    }

DBA Adapter

private static final String VALUE_TABLE = "tracked_value";
    public static final String VAL_ID = "fa_id";
    public static final String VALUE= "t_value";

public long insertLC(String t_value)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(VALUE, t_value);
        return  mDb.insert(VALUE_TABLE, null, initialValues);

    }

DB Helper

public class DBHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "NEW.db";
    private static final int DATABASE_VERSION = 1;

    private static final String VALUE_TABLE = "tracked_value";
    public static final String VAL_ID = "fa_id";
    public static final String VALUE= "t_value";

private static final String CREATE_VALUE_TABLE = "CREATE TABLE "+VALUE_TABLE+"  ("+VAL_ID+" integer primary key autoincrement,"+VALUE+" varchar);";

@Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(CREATE_VALUE_TABLE);

    }
}

i already have insert text value to table.but i don't know how to insert image to sqlite table.

Yasi
  • 23
  • 6
  • 2
    instead of inserting image in db..i would suggest you to insert image url in DB.. – Meenal Sep 10 '15 at 10:43
  • Agree with @MeenalSharma, You can store local file path in to DB and reuse it when ever you want..If you feel its mandatory for you fallow below link : http://stackoverflow.com/questions/7331310/how-to-store-image-as-blob-in-sqlite-how-to-retrieve-it – Guruprasad Sep 10 '15 at 10:46
  • Even on servers inserting an image into a DB is a very bad idea. It's a horrible idea when the DB is sqlite and when it's on a phone. Please do as @MeenalSharma says – e4c5 Sep 12 '15 at 02:04

0 Answers0