【发布时间】: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 class的onActivityResult方法中调用了这个方法
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