Saving Image path from gallery to Room Database and display it in Recycler list

前端 未结 2 1695
夕颜
夕颜 2021-01-05 11:53

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

相关标签:
2条回答
  • 2021-01-05 12:01

    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);
    
    0 讨论(0)
  • 2021-01-05 12:12

    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()));
    
    0 讨论(0)
提交回复
热议问题