我正在尝试创建一个用户上传图片并将其保存在 SQLite 数据库中的应用程序。到目前为止,我已经成功地按照教程将图像上传到应用程序,但是我不确定如何将其保存在 SQLite 中,以便当活动关闭并再次打开时,图像会保留在那里。
我的代码:
public class Getimg2 extends Activity implements View.OnClickListener{
private static final int RESULT_LOAD_IMAGE =1;
ImageView ImageUp;
Button BtnUp;
Button Confirm;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diary_edit);
ImageUp = (ImageView) findViewById (R.id.imageUp);
BtnUp = (Button) findViewById (R.id.btnUp);
Confirm =(Button) findViewById (R.id.confirm) ;
ImageUp.setOnClickListener(this);
BtnUp.setOnClickListener(this);
Confirm.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch(v.getId()) {
case R.id.imageUp:
Intent galleryIntent = new Intent (Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent,RESULT_LOAD_IMAGE);
break;
case R.id.btnUp:
Bitmap image =((BitmapDrawable)ImageUp.getDrawable()).getBitmap();
break;
case R.id.confirm:
//save image to sql?
Cursor c =dba.rawQuery("select * from tb", null);
if (c.moveToNext())
break;
}
}
@Override
protected void onActivityResult(int requestCode , int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data !=null){
Uri selectedImage =data.getData();
ImageUp.setImageURI(selectedImage);
}
}
private class UploadImage extends AsyncTask<Void,Void ,Void >{
Bitmap image;
String name;
public UploadImage(Bitmap image,String name){
this.image = image;
this.name = name;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);
}}}