Reputation: 357
I am taking a picture from the gallery and want to save this into my SQLite database. I created a method insertData(int Image, String Name)
in my SQ_Lite_DB
which extends the SQLiteOpenHelper
class.
insertData method
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;
}
I am calling this method in my onActivityResult
method of sq-lite class
SQLite class
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());
}
}
}
}
Upvotes: 0
Views: 161
Reputation: 1520
Try with following code, you can add one more column in you table for imageUri and save imagepath.
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();
}
}
}
}
Upvotes: 1