如何在android sqlite数据库中插入和检索捕获图像

时间:2015-09-28 05:03:55

标签: java android eclipse sqlite

我的代码仅在运行时运行。但当我关闭我的应用程序并再次打开时,我没有得到图像,我收到应用程序关闭错误。我该如何使用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();
        }
    }
}

这是我的xml代码:

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

4 个答案:

答案 0 :(得分:1)

在调用rawQuery()之前,您需要使用db=mdb.getReadableDatabase();内部显示单击侦听器。

为您提供日志以获取更多详细信息。

答案 1 :(得分:0)

可能db按钮点击导致问题,因为rawQuery在调用db=mdb.getWritableDatabase(); // missing c=db.rawQuery("....."); 方法之前未初始化:

    Document d;
    String url="your_url";
    d = Jsoup.connect(url).get();
    Element p = d.select("body p").first(); // fetch from p tag content
    String jsonString = p.text();

答案 2 :(得分:0)

在调用db.query

之前,尝试检查参数是否为null

答案 3 :(得分:0)

请检查

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

有关详细信息,请查看https://randula.wordpress.com/2013/01/01/android-save-images-to-database-sqlite-database/