There are a list of students.each row shows an Image,Name and number. I created a Room database and only managed to populate \"name\" and \"number\" columns into the list us
To be honest the process isn't that much different with Room. As you said, to pick the photo from the Gallery you use an intent:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "select a picture"), YOUR_IMAGE_CODE);
Then you handle this case in onActivityResult
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == YOUR_IMAGE_CODE) {
if(resultCode == RESULT_OK)
Uri selectedImageUri = data.getData();
}
}
So selectedImageUri
is the piece of information that you save in your database. In your Entity class Student.java
you can change mStudentPic
to String
so when you insert your Uri
you can use a method:
selectedImageUri.toString();
and when you want to convert it back to Uri
:
Uri uri = Uri.parse(yourUriAsString);
I assumed that you know how to insert and query values from the database.
And then in your onBindViewHolder
you can use Glide or Picasso, or any other library to load the image, for example with Glide:
Glide.with(context)
.load(new File(uri.getPath()))
.into(imageView);
Also, I am using the same function with SQLite I am the storing just image path and get from SQLite and load image using image URI
holder.imgUserPhoto.setImageURI(Uri.parse(new File(contactModel.getContactPhoto()).toString()));