我正在构建一个博客应用程序,该应用程序允许用户为博客文章添加图片。我当前正在将数据存储为URI字符串,但是我的问题是图像仅在首次添加时显示,如果刷新页面或重新启动应用程序,则图像将不会显示,只是空白而没有图像。 / p>
@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
}
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()
}
}
我看过一些有关使用blob的文章,但大多数内容不清楚,而blob仅允许图像大小限制为1MB的事实使我们希望有一个更好的选择。 请帮助更好地保存和还原图像。