【问题标题】:Getting picture from user in Android studio and encoding so it can be stored in the DB在 Android Studio 中从用户那里获取图片并进行编码,以便可以将其存储在数据库中
【发布时间】:2021-12-31 12:37:21
【问题描述】:

我从用户那里得到一张图片,该图片被转换为位图。然后我将位图转换为字节数组并通过 JSON 发送以存储在数据库中。然后,当用户启动特定活动时,我希望从数据库中检索图像并显示给用户。

在应用程序中,用户可以发布带有标题、描述和图像的帖子。我希望将这三个变量存储在数据库中,以便其他人查看帖子时,他们能够看到所有内容。此外,图像将作为 blob 存储在数据库中,我只需使用 JSON 将数据发送到处理与数据库的所有通信的后端应用程序。

我的问题是,我得到的位图似乎是对设备 android.graphics.Bitmap@324a72b 上某些内存的引用,尽管我选择了相同的图像,但每次运行应用程序时它都会发生变化。我希望能够检索实际的位图,以便将其存储在数据库中。我也没有将其用作存储图像的网络服务器,因为它是一个较小的项目。

b.buttonNewItemUpImg.setOnClickListener {
            val openGalleryIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
            startActivityForResult(openGalleryIntent, ResultLoadImage)
        }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == ResultLoadImage){
            if (resultCode == Activity.RESULT_OK){
                var temp = MediaStore.Images.Media.getBitmap(this.contentResolver, data!!.getData())
                bitmap = getResizedBitmap(temp!!, maxImageSize)
                b.imageView.setImageURI(data!!.getData())
            }
        }
    }

getResizedBitmap() 函数只是让图像变小。

【问题讨论】:

  • get an image from an user which is converted into a bitmap. I then convert the bitmap to a an byte array 我想你从用户那里得到了一个 jpg 文件。如果您从中制作位图,然后将其压缩为字节数组,则字节数组包含 jpg 文件的字节。那么为什么要采取所有这些不必要的行动呢?您也不需要 json 将数组放入数据库中,但可以将其存储为 blob。
  • Getting picture from user .... Ahum... 没有用户向您发送图片。您让应用程序的用户使用 ACTION_PICK 选择图像。相当混乱的标题。
  • 抱歉解释。我现在意识到这还不够。在应用程序中,用户可以发布带有标题、描述和图像的帖子。我希望将这三个变量存储在数据库中,以便其他人查看帖子时,他们能够看到所有内容。此外,图像将作为 blob 存储在数据库中,我只需使用 JSON 将数据发送到处理与数据库的所有通信的后端应用程序。
  • 现在,如果您将该评论的文本放在帖子的开头...

标签: android arrays android-studio kotlin bitmap


【解决方案1】:

https://stackoverflow.com/a/57576381/6359367中所述

使用这个

      var temp = ImageDecoder.createSource(this.getContentResolver(), data!!.getData())
      bitmap = ImageDecoder.decodeBitmap(temp);

注意这段代码是 java 我试图将它改编为 kotlin 你可能需要稍微调整一下

【讨论】:

    【解决方案2】:

    您应该将图像复制到应用程序的本地存储中,然后将这些复制图像的路径存储到数据库中,而不是将整个图像存储在数据库中,以便以后检索图像。

    获取选中图片后复制图片到应用本地存储uri

    //creating an image file in the app's local storage
    val dir = requireActivity().getDir(FOLDER_TO_SAVE, Context.MODE_PRIVATE).path
    val copiedImageFile = File(dir, UUID.randomUUID().toString() + ".jpg")
    val copiedImageUri = Uri.fromFile(copiedImageFile)
    
    //copying selected image to the local storage
    data!!.getData().path?.let {
        File(it).copyTo(copiedImageFile)
    }
    

    使用uri获取上述复制图片的路径

    copiedImageUri.path.?.let{
         val copiedImagePath = "file://$it"
         //now save this path to your database
    }
    

    使用此保存的图像路径检索任何片段或活动中的图像。

    【讨论】:

    • 这可行,但图像需要存储在数据库中,以便其他用户可以访问并查看图像。
    • 其他用户是什么意思?应用的数据库和本地存储只能由应用访问。
    • 我不是指应用程序数据库。我有一个外部数据库,我希望在其中存储这些图像,以便在其他用户启动将显示这些图像的活动时检索它们。
    猜你喜欢
    • 2020-03-03
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    相关资源
    最近更新 更多