【问题标题】:how to pass an image Uri or alternatively a bitmap to int parameter of SQLite database to save an image?如何将图像 Uri 或位图传递给 SQLite 数据库的 int 参数以保存图像?
【发布时间】:2021-06-19 11:13:11
【问题描述】:

我正在从图库中拍摄一张照片,并希望将其保存到我的 SQLite 数据库中。我在SQ_Lite_DB 中创建了一个方法insertData(int Image, String Name),它扩展了SQLiteOpenHelper 类。

插入数据方法

public boolean insertData(int Image, String Name){
    SQLiteDatabase database = getReadableDatabase();
    ContentValues values = new ContentValues();
    values.put("image",Image);
    values.put("name",Name);

   long id =  database.insert("orders",null,values);
    database.close();
    return id > 0;
}

我在sq-lite classonActivityResult方法中调用了这个方法

SQLite 类

public class SQLite extends AppCompatActivity {

RecyclerView recyclerView;
Button insertdatabtn;
EditText edittext;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sqlite);

    insertdatabtn = findViewById(R.id.insertdatabtn);

    new SQ_Lite_DB(SQLite.this).ReadSqliteData(SQLite.this);

    ArrayList<Model> list = new ArrayList<>();
    Adpter adpter = new Adpter(list,SQLite.this);
    recyclerView = findViewById(R.id.recyclerview);

    recyclerView.setAdapter(adpter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));


    insertdatabtn.setOnClickListener(v->{
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent,11);});
}

@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    edittext = findViewById(R.id.edittext);

    SQ_Lite_DB db = new SQ_Lite_DB(SQLite.this);

    if (resultCode==RESULT_OK&& requestCode==11){

        try {InputStream inputStream = getContentResolver().openInputStream(data.getData());
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG,100,baos);

            byte[] img = baos.toByteArray();

           database.inserdata(/*what to put here, a int value needed but I can get uri or bitmap only*/,editText.getText.toString)
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
            Log.d("tag",e.getMessage());
        }
    }
}
}

【问题讨论】:

标签: java android sqlite android-sqlite


【解决方案1】:

尝试使用以下代码,您可以在表中为 imageUri 添加一列并保存图像路径。

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, 100);
        }
    });


 //The onActivityResult method will invoke after select image from gallery
 @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    switch (requestCode){
        case 100:
            if(resultCode == RESULT_OK){
                try {
                    final Uri imageUri = imageReturnedIntent.getData();
                   String  imageUri = imageUri.toString();
                } catch (FileNotFoundException e){
                    e.printStackTrace();
                }
            }
     }
  }

【讨论】:

  • 也许你想说我应该存储图像的Uri。好的,我可以保存它,但是如何从 uri 中检索和显示图像。我的意思是可以使用 Uri 保存和检索图像吗?
  • 是的,您可以保存 imageUri,也可以使用该 uri 在视图中显示图像
  • 使用这个属性 imageView.setImageURI(imageUri)
  • 能否请您回答这个问题,以便我品尝您的解决方案? stackoverflow.com/questions/68053602/…
  • 谢谢兄弟!你帮了我很多!如果可能,请为我的问题投票。
猜你喜欢
  • 1970-01-01
  • 2020-02-10
  • 2020-11-30
  • 2013-10-04
  • 2012-12-16
  • 2016-08-03
  • 2015-11-15
  • 1970-01-01
  • 2011-06-29
相关资源
最近更新 更多