Android Studio使用ImagePath将图像保存并检索到sqlite数据库

时间:2017-07-12 18:51:18

标签: android sqlite android-sqlite

在我的应用程序中,我必须使用sqlite数据库的路径保存图像,因为当我将其保存为BLOB时,sqlite会出错。这个错误是一个CursowWindow。 根据研究,SQLite在一行上不支持超过2MB的数据。因此,我必须改变方法。

  

DBTABLE

db.execSQL("create table questions(_id integer primary key autoincrement," +
            "stuId integer," +
            "lesson text not null," +
            "subject text not null," +
            "questPhoto blob," +
            "answer text not null," +
            "questTime text," +
            "point integer not null)");

图像将保存在“questPhoto”中。

  

sorupaylas.class

我添加到图像数据库的课程。

我用来将信息保存到数据库的方法。

ContentValues cv=new ContentValues();
    cv.put("questTime",getDateTime().toString());
    cv.put("stuId",id_degeri);
    cv.put("lesson",txtdegiskenkontrol.getText().toString());
    cv.put("subject",konusec.getText().toString());
    cv.put("answer",textdogrucevap.getText().toString());
    cv.put("point",textsorupuan.getText().toString());
    cv.put("questPhoto",imageViewToByte(imageView));
    long id = mSQLiteDb.insert("questions",null,cv);
    Toast.makeText(sorupaylas.this, String.valueOf(id), Toast.LENGTH_SHORT).show();

private byte[] imageViewToByte(ImageView imageView) {
    Bitmap bitmap=((BitmapDrawable)imageView.getDrawable()).getBitmap();
    ByteArrayOutputStream stream= new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG,100,stream);
    byte[] byteArray =stream.toByteArray();
    return byteArray;
}

和onActivityResult

private void GalleryOpen() {
    GalIntent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(Intent.createChooser(GalIntent,"Select Image from Gallery"),2);
}

private void CameraOpen() {
    CamIntent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    file=new File(Environment.getExternalStorageDirectory(),"file"+String.valueOf(System.currentTimeMillis())+".jpg");
    uri=Uri.fromFile(file);
    CamIntent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
    CamIntent.putExtra("return-data",true);
    startActivityForResult(CamIntent,0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == 0 && resultCode == RESULT_OK)  // Open Camera
        CropImage();
    else if(requestCode == 2){    // Open Gallery
        if(data!= null){
            uri=data.getData();
            CropImage();
        }
    }
    else if(requestCode == 1){
        if(data !=null){
            Bundle bundle=data.getExtras();
            Bitmap bitmap= (Bitmap) bundle.get("data");

            Picasso.with(sorupaylas.this)
                    .load(uri)
                    .resize(1100,1100)
                    .centerCrop()
                    .into(imageView);
            imageView.setImageBitmap(bitmap);
        }
    }
}

我正在获取此代码行的图像。

byte[] getphotoOfQuest=cursor.getBlob(cursor.getColumnIndexOrThrow("questPhoto"));
                bitmap=BitmapFactory.decodeByteArray(getphotoOfQuest,0,getphotoOfQuest.length);

我会等你的建议。

0 个答案:

没有答案