将图像路径从图库保存到房间数据库,并在“回收者"列表中显示 [英] Saving Image path from gallery to Room Database and display it in Recycler list

查看:60
本文介绍了将图像路径从图库保存到房间数据库,并在“回收者"列表中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个学生列表.每行显示一个图像,名称和编号. 我创建了一个Room数据库,仅使用

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 using this guide.

当用户打开AddNewStudentActivity时,他/她需要从图库中选择一张照片,并填写两个editTexts作为名称和编号,然后单击保存",然后将其保存到StudentDatabase.

When user opens the AddNewStudentActivity, He/She needs to choose a photo from gallery and fill two editTexts for the name and number and click "save" and save to student to the studentDatabase.

图像应在列表中显示在这两个文本(名称和数字)旁边.

the image should be displayed in the list alongside those two texts(name and number).

我不知道如何执行此操作,我只认为过程应该像设置打开画廊的意图,我们可以选择图像并将其路径存储在数据库中,将其从数据库显示到列表中",但不知道如何编写代码.有关于此的教程,但是它们都使用SQLITE而不是Room,并且我是整个数据库主题的新手.

I have NO IDEA how to do this i only think the process should be like "setting up an intent that opens the gallery and we can choose an image and get it's path stored on the database and display it from database to the list"but don't know how to code it.there are tutorials about this but they were all using SQLITE and not Room and I'm new to the whole database topic.

-谢谢

NewStudentActivity.java

NewStudentActivity.java

public class NewStudentActivity extends AppCompatActivity {

    public static final String EXTRA_REPLY = "com.example.android.studentlistsql.REPLY";

    private EditText mNameWordView;
    private EditText mNumWordView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_student);
        mNameWordView = findViewById(R.id.name_word);
        mNumWordView = findViewById(R.id.num_word);

        final Button button = findViewById(R.id.button_save);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent replyIntent = new Intent();
                if (TextUtils.isEmpty(mNameWordView.getText())) {
                    setResult(RESULT_CANCELED, replyIntent);
                } else {
                    String word = mNameWordView.getText().toString();
                    replyIntent.putExtra(EXTRA_REPLY, word);
                    setResult(RESULT_OK, replyIntent);
                }
                if (TextUtils.isEmpty(mNumWordView.getText())) {
                    setResult(RESULT_CANCELED, replyIntent);
                } else {
                    String word = mNumWordView.getText().toString();
                    replyIntent.putExtra(EXTRA_REPLY, word);
                    setResult(RESULT_OK, replyIntent);
                }
                finish();
            }
        });

    }
}

StudentListAdapter.java

StudentListAdapter.java

public class StudentListAdapter extends RecyclerView.Adapter<StudentListAdapter.WordViewHolder> {

    class WordViewHolder extends RecyclerView.ViewHolder {
        private final TextView nameItemView;
        private final TextView numberItemView;

        private WordViewHolder(View itemView) {
            super(itemView);
            nameItemView = itemView.findViewById(R.id.nameTextView);
            numberItemView = itemView.findViewById(R.id.numberTextView);
        }
    }

    private final LayoutInflater mInflater;
    private List<Student> mStudents; // Cached copy of words

    StudentListAdapter(Context context) { mInflater = LayoutInflater.from(context); }

    @Override
    public WordViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.recyclerview_item, parent, false);
        return new WordViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(WordViewHolder holder, int position) {
        if (mStudents != null) {
            Student current = mStudents.get(position);
            holder.nameItemView.setText(current.getStudentName());
            holder.numberItemView.setText(current.getStudentNumber());
        } else {
            // Covers the case of data not being ready yet.
            holder.nameItemView.setText("No Word");
        }
    }

    void setStudents(List<Student> words){
        mStudents = words;
        notifyDataSetChanged();
    }

    // getItemCount() is called many times, and when it is first called,
    // mWords has not been updated (means initially, it's null, and we can't return null).
    @Override
    public int getItemCount() {
        if (mStudents != null)
            return mStudents.size();
        else return 0;
    }
}

推荐答案

老实说,与 Room 相比,该过程没有太大不同.如您所说,要从图库中选择照片,请使用以下意图:

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);

然后您在onActivityResult中处理这种情况:

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();
    }
}

因此selectedImageUri是您保存在数据库中的一条信息.在您的实体类Student.java中,您可以将mStudentPic更改为String,因此,当您插入Uri时,可以使用以下方法:

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();

,以及要将其转换回Uri的时间:

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.

然后在onBindViewHolder中,可以使用 Glide

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);

这篇关于将图像路径从图库保存到房间数据库,并在“回收者"列表中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆