wingsraam
wingsraam

Reputation: 187

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

My code is working only at run time. But when I close my app and open again, I don't get an image, I get an app closed error. How should I be using rawquery?

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 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>

Upvotes: 0

Views: 692

Answers (4)

Ashish Agrawal
Ashish Agrawal

Reputation: 1977

Please check this

public void insetImage(Bitmap bitmap, String imageId)  {
 SQLiteDatabase db = this.getWritableDatabase();
 ContentValues values = new ContentValues();
 values.put(IMAGE_ID, imageId);
 ByteArrayOutputStream stream = new ByteArrayOutputStream();
 bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
 values.put(IMAGE_BITMAP, stream.toByteArray());
 db.insert(TABLE_IMAGE, null, values);
 db.close();
 }


 public ImageHelper getImage(String imageId)  {
 SQLiteDatabase db = this.getWritableDatabase();
 Cursor cursor2 = db.query(TABLE_IMAGE,
 new String[] {COL_ID, IMAGE_ID, IMAGE_BITMAP},IMAGE_ID
 +" LIKE '"+imageId+"%'", null, null, null, null);
 ImageHelper imageHelper = new ImageHelper();
 if (cursor2.moveToFirst()) {
 do {
 imageHelper.setImageId(cursor2.getString(1));
 imageHelper.setImageByteArray(cursor2.getBlob(2));
 } while (cursor2.moveToNext());
 }
 cursor2.close();
 db.close();
 return imageHelper;
 }

for more info you can check on https://randula.wordpress.com/2013/01/01/android-save-images-to-database-sqlite-database/

Upvotes: 0

prat
prat

Reputation: 647

Try to check if parameter is null before calling db.query

Upvotes: 0

Parth
Parth

Reputation: 137

Inside display click listener you need to use db=mdb.getReadableDatabase(); before calling rawQuery().

Provide you logs for more details.

Upvotes: 1

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

Probably display Button click causing issue because db is not initialised before calling rawQuery method:

db=mdb.getWritableDatabase(); // missing 
c=db.rawQuery(".....");

Upvotes: 0

Related Questions