【问题标题】:How do I get the actual image taken from a camera in Android Studio?如何在 Android Studio 中获取从相机拍摄的实际图像?
【发布时间】:2023-01-22 21:28:54
【问题描述】:

我正在使用 Android Studio 中的相机拍照,我想保存操作产生的实际图像。我可以很好地访问 URI,但我想要实际图像本身,因为我需要将照片发送到数据库。

    var image_uri: Uri? = null
    lateinit var bitmap: Bitmap
    
    private fun openCamera() {
        val resolver = requireActivity().contentResolver
        val values = ContentValues()
        values.put(MediaStore.Images.Media.TITLE, "New Picture")
        values.put(MediaStore.Images.Media.DESCRIPTION, "From the Camera")
        image_uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)

        bitmap = MediaStore.Images.Media.getBitmap(resolver, image_uri)

        val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri)
        startActivityForResult(cameraIntent, IMAGE_CAPTURE_CODE)
    }

我读过最简单的方法是创建位图,但我无法让它工作。运行我的整个程序,只要调用 openCamera,应用程序就会崩溃。如果我注释掉位图行,那么该函数就可以正常工作(除非我没有像我想要的那样保存文件)。我怎样才能做到这一点,位图是我可以发送到程序后端的实际位图对象?

【问题讨论】:

  • 在调用 onActivityResult() 方法之前,您无法使用该图像。因此,请尝试使用图像。请注意,如果您的目标是将图像发送到服务器,加载 Bitmap 可能对您没有多大用处,因为您没有很好的方法将其发送到服务器。相反,upload the image from the Uri directly

标签: android android-studio kotlin bitmap


【解决方案1】:

您可以通过以下方式从 Camera 获取图像位图:

// Open camera
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
resultLauncher.launch(cameraIntent)

// Get your image
private val resultLauncher =
    registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            if (result?.data != null) {
                bitmap = result.data?.extras?.get("data") as Bitmap
            }
        }
    }

【讨论】:

    【解决方案2】:

    获取位图的最简单方法是在 onActivityResult() 中,例如 val imageBitmap = data.extras.get("data") as Bitmap。我建议查看相机文档,也许您会找到有用的东西here

    【讨论】:

      【解决方案3】:

      获取实际图像的方法是传递要存储图像的文件对象。

      根据android developers documentation

      你应该创建文件(假设你有 READ_EXTERNAL_STORAGE 和 WRITE_EXTERNAL_STORAGE 权限,具体取决于 android 版本和你创建的文件的位置......)然后将文件传递给 intent

      private fun dispatchTakePictureIntent() {
      Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
          // Ensure that there's a camera activity to handle the intent
          takePictureIntent.resolveActivity(packageManager)?.also {
              // Create the File where the photo should go
              val photoFile: File? = try {
                  createImageFile()
              } catch (ex: IOException) {
                  // Error occurred while creating the File
                  ...
                  null
              }
              // Continue only if the File was successfully created
              photoFile?.also {
                  val photoURI: Uri = FileProvider.getUriForFile(
                          this,
                          "com.example.android.fileprovider",
                          it
                  )
                  takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
                  startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
              }
          }
      }
      

      }

      在代码 sn-p 中,它引用了正在创建文件的方法“createImageFile()”(链接中的文档提供了一些示例)。

      【讨论】:

        猜你喜欢
        • 2011-07-15
        • 2021-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-10
        • 1970-01-01
        • 2015-03-07
        • 1970-01-01
        相关资源
        最近更新 更多