1
我想創建一個應用程序,用戶上傳圖片並將其保存在SQLite數據庫中。到目前爲止,我已經成功設法上傳圖像到應用程序,同時遵循教程,但是我不確定如何將其保存在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);
}}}