简体   繁体   English

如何从房间数据库中存储和检索图像

[英]How to store and retrieve image from room database

I am building a blog application that allows a user to add picture for the blog post.我正在构建一个博客应用程序,允许用户为博客文章添加图片。 I am currently storing the data as URI string but the issue I have is that the image only shows when I first add it, if I refresh the page or relaunch the application the image will not show, just a blank space with no image.我目前将数据存储为 URI 字符串,但我遇到的问题是图像仅在我第一次添加时显示,如果我刷新页面或重新启动应用程序,图像将不会显示,只是一个没有图像的空白区域。

Database Entity数据库实体

@Entity
class PostEntity(
    var title: String,
    var body: String,
    var image: String? = null,
    var date: String?

): Serializable {

    @PrimaryKey(autoGenerate = true)
    var id: Int = 0

    var likes:Int? = 0



}

Capturing Images from gallery and camera and converting to URI string从画廊和相机捕获图像并转换为 URI 字符串

    private fun openCamera(){
        try{
            val CAMERA_REQUEST = 200
            val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            startActivityForResult(intent, CAMERA_REQUEST)
        }
        catch (e:Exception){
            Toast.makeText(context, "Please try again: ${e.message}", Toast.LENGTH_SHORT).show()
        }

    }

// function to be called in onActivityResult
    private fun loadImage(requestCode: Int, imageView: ImageView, context: Context, data: Intent?){
        if(requestCode == 200){
            try{

                val image = data!!.extras?.get("data") as Bitmap


                imageView.setImageBitmap(image)
                imageView.visibility = View.VISIBLE

                imageUriLoader = getImageUriFromBitmap(context, image)
            }
            catch (e:Exception){
                Toast.makeText(context, "Please try againt: ${e.message}", Toast.LENGTH_SHORT).show()
            }

        }

        else if(requestCode == 201){
            try{
                val imageUri = data!!.data
                imageUriLoader = imageUri
                Picasso.get().load(imageUri).into(imageView)

                imageView.visibility = View.VISIBLE
                imageView.setImageURI(imageUri)

            }
            catch (e:Exception){
                Toast.makeText(context, "Please try again: ${e.message}", Toast.LENGTH_SHORT).show()
            }

        }
        else{
            Toast.makeText(context, "Invalid request", Toast.LENGTH_SHORT).show()
        }
    }

I have seen some post about using blob, most of them are not clear and the fact that blob only allows image size limit of 1MB makes want a better option.我看过一些关于使用 blob 的帖子,其中大部分都不清楚,而且 blob 只允许 1MB 的图像大小限制这一事实使得想要一个更好的选择。 Please kindly help with a better to save and restore images.请帮助更好地保存和恢复图像。

In your database you should have a field of type String (or the equivalent VARCHAR etc.) that serves as image path inside a directory of your choice, so for example在您的数据库中,您应该有一个字符串类型的字段(或等效的 VARCHAR 等)作为您选择的目录中的图像路径,例如

在此处输入图片说明

Then you can add whatever fields you need, for example in your case you would need a userID that serves as a foreign key to the user logged in. So as soon as the user loads an image you save the file to the path you desire and then save that same path inside the db so that next time the user loggs in you look up his image path and then load it on the web page.然后您可以添加您需要的任何字段,例如在您的情况下,您需要一个用作登录用户的外键的用户 ID。因此,一旦用户加载图像,您就可以将文件保存到您想要的路径,然后然后将相同的路径保存在数据库中,以便下次用户登录时查找他的图像路径,然后将其加载到网页上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM