gpt4 book ai didi

java - 如何将图像上传到 SQLite 数据库?

转载 作者:行者123 更新时间:2023-11-30 01:23:07 25 4
gpt4 key购买 nike

我正在尝试创建一个应用程序,用户可以在其中上传图片并将其保存在 SQLite 数据库中。到目前为止,我已经按照教程成功地将图像上传到应用程序,但是我不确定如何将它保存在 SQLite 中,以便在 Activity 关闭并再次打开时图像保留在那里。

我的代码:

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

最佳答案

在数据库中存储如此大的项目不是正确的方法。将文件存储在应用程序的私有(private)文件系统中,然后将文件的路径存储在数据库中。

关于java - 如何将图像上传到 SQLite 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36803836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com