dimanche 27 septembre 2015

How to insert and Retrieve for capture image in android sqlite database

My Code here

my code is working good only at the run time. But when i closed once my app and again open i didn't get image, only i got unfortunately app closed error. please help me how to use rawquery here at the all time.

public static final int REQUEST_IMAGE=1;
EditText _id,_name;
ImageView img,img1;
Button insert,display;
File desi;
String imagepath;
private MyDataBase mdb=null;
private SQLiteDatabase db=null;
private Cursor c=null;
private byte[] imge=null;
private static final String DATABASE_NAME = "sampledp.db";
public static final int DATABASE_VERSION = 1;
private String sid;
static String name;

TextView idid;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_db);
    _id=(EditText)findViewById(R.id.edit_id);
    _name=(EditText)findViewById(R.id.edit_name);
    img=(ImageView)findViewById(R.id.image);
    img1=(ImageView)findViewById(R.id.image1);
    insert=(Button)findViewById(R.id.insert);
    idid=(TextView)findViewById(R.id.idid);
    display=(Button)findViewById(R.id.display);
    mdb=new MyDataBase(getApplicationContext(), DATABASE_NAME,null, DATABASE_VERSION);

    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            name=_id.getText().toString();
            desi=new File(Environment.getExternalStorageDirectory(),name+".JPEG");
            Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(desi));
            startActivityForResult(i, REQUEST_IMAGE);
        }
    });
    insert.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View arg0) {
            Bitmap b=BitmapFactory.decodeFile(imagepath);
            ContentValues cv=new ContentValues();
            ByteArrayOutputStream bos=new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.JPEG, 90, bos);
            imge=bos.toByteArray();
            sid=_id.getText().toString();
            db=mdb.getWritableDatabase();
            cv.put("image", imge);
            cv.put("ID", sid);
            db.insert("tableimage", null, cv);
            Toast.makeText(getApplicationContext(), "inserted successfully", Toast.LENGTH_SHORT).show();
        }
    });
    display.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String[] col={"image","ID"};
            c=db.rawQuery("select * from tableimage where ID="+_name.getText()+";",col);
            if(c!=null){
                c.moveToFirst();
                do{
                    imge=c.getBlob(c.getColumnIndex("image"));
                    sid=c.getString(c.getColumnIndex("ID"));
                }while(c.moveToNext());
            }
            Bitmap b1=BitmapFactory.decodeByteArray(imge, 0, imge.length);

            String ii=String.valueOf(sid);


            img1.setImageBitmap(b1);
            idid.setText(ii);
            Toast.makeText(getApplicationContext(), "Retrive successfully", Toast.LENGTH_SHORT).show();
        }
    });
}


protected void onActivityResult(int requestCode,int resultCode,Intent data){
    if(requestCode==REQUEST_IMAGE && resultCode==Activity.RESULT_OK){
        try{
            FileInputStream in=new FileInputStream(desi);
            imagepath=desi.getAbsolutePath();
            Bitmap bb=BitmapFactory.decodeStream(in);
            img.setImageBitmap(bb);

        }catch(FileNotFoundException e){
            e.printStackTrace();
        }
    }
}

This is the my xml code

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/edit_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="roll No" />

        <ImageView
            android:id="@+id/image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/ic_launcher" />

        <Button
            android:id="@+id/insert"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Insert Details" />

        <EditText
            android:id="@+id/edit_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="enter roll No" />

        <Button
            android:id="@+id/display"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Display Details" />

        <ImageView
            android:id="@+id/image1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/idid"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="id" />
    </LinearLayout>

Aucun commentaire:

Enregistrer un commentaire