当前位置: 首页 > 知识库问答 >
问题:

将图像路径从画廊保存到房间数据库,并显示在回收者列表中

桂梓
2023-03-14

有一个students.each行列表显示图像、名称和号码。我创建了一个房间数据库,并且只设法使用本指南将“姓名”和“号码”列填充到列表中。

用户打开AddNew生学生活动时,他/她需要从图库中选择一张照片并填写两个编辑文本以获取姓名和号码,然后单击“保存”并将其保存到学生数据库。

图像应与这两个文本(姓名和号码)一起显示在列表中。

我不知道如何做到这一点,我只是觉得这个过程应该像“设置一个意图,打开画廊,我们可以选择一个图像,并得到它的路径存储在数据库中,并显示它从数据库到列表”,但不知道如何编码它。有关于这方面的教程,但它们都使用SQLITE,而不是Room,我对整个数据库主题还不熟悉。

-谢谢

新闻学生活动。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();
            }
        });

    }
}

dapter.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;
    }
}

共有2个答案

樊运乾
2023-03-14

此外,我正在使用与SQLite相同的功能,我是存储只是图像路径,并从SQLite获取并使用图像URI加载图像

holder.imgUserPhoto.setImageURI(Uri.parse(new File(contactModel.getContactPhoto()).toString()));
姜良哲
2023-03-14

老实说,这个过程和房间没什么不同。正如你所说,要从图库中挑选照片,你需要使用一个意图:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "select a picture"), YOUR_IMAGE_CODE);

然后在活动结果中处理此情况:

@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是保存在数据库中的一段信息。在实体类学生中。java您可以将mStudentPic更改为String,因此当您插入Uri时,可以使用以下方法:

selectedImageUri.toString();

当你想把它转换回Uri

Uri uri = Uri.parse(yourUriAsString);

我假设您知道如何从数据库中插入和查询值。

然后在您的onBindViewHolder中,您可以使用Glide或Picasso或任何其他库来加载图像,例如使用Glide:

Glide.with(context)
.load(new File(uri.getPath()))
.into(imageView);
 类似资料:
  • 我在后端使用Rails API,在前端使用ReactJS。我也使用载波在后端处理我的图像。我试图上传我的图像并将其发送到后端。使用简单的超文本标记语言上传器在前端捕获图像文件。 这是我的uploader.jsx 以上代码是此解决方案的精确副本。我使用formData和axios将数据推送到后端。但数据在数据库中存储为“nil”值。安慰log(formData)给出一个空哈希。 我在后端的控制器代码

  • 我是PHP的新手,需要在一个项目中弄清楚这一点--我用HTML制作了一个image submit表单,它将div中的img更改为使用该表单选择的图像。下面是我如何完成的: 而且 我需要将这个图像上传到我为每个用户在MySql数据库中的medium Blob创建的列中。我尝试了许多不同的方法(尝试了等),但不知道我在做什么。 我现在正在学习本教程-http://www.sevenkb.com/php

  • 我目前正在尝试跟踪一个人的动作,并将其保存到数据库中。 在我的网页上有一个画布,它允许用户使用鼠标进行绘画。我希望能够保存用户在绘画时所做的动作,以便我能够重新跟踪绘画时所做的每一个动作。 我自己的想法是,每当用户在画布中单击鼠标时,坐标将被保存,直到用户释放鼠标按钮。另一个解决方案是,在画布上每点击3-4次,就保存一张画布的图像,这样你就可以看到绘制过程。 有没有人有更好的解决方案,或者关于如何

  • 问题内容: 我的客户在php + mysql中创建了一个脚本,该脚本将图像直接保存在数据库中,并且每个图像都有这样的url:www.example.com/image.php?id=421 您认为这是一个非常错误的解决方案?我应该重建所有站点吗? 每天大约有1000次访问,数据库中大约有600张图像。 问题答案: 图像文件是文件,除非有充分的理由将它们存储在数据库中,否则它们应属于文件系统,在文件

  • 你好,我正在制作员工目录。在这种形式下,我想上传员工的照片,当我试图存储图像时,我遇到了错误,比如Portlet暂时不可用,如果我成功删除图像上传逻辑,然后在数据库中删除其他数据存储。请帮助我如何在数据库中存储图像路径。我已在/webapps中创建图像文件夹。我想将我的所有图像存储在此文件夹中,并将图像路径存储到数据库中。请告诉我如何存储图像路径,以及存储图像路径后如何在其他页面中显示图像。 在这

  • 所以,基本上,我需要上传单个图像,将其保存到localStorage中,然后在下一页上显示它。 目前,我有我的HTML文件上载: 它使用该函数在页面上显示图像 图像会立即显示在页面上供用户查看。然后要求他们填写表格的其余部分。这部分工作很好。 一旦表格完成,他们然后按下一个“保存”按钮。按下此按钮后,我将所有表单输入保存为字符串。我需要一种方法来将图像保存为项。 save按钮还将引导他们进入一个新