简体   繁体   English

使用 Kotlin 从相机/图库中捕获图像并在活动/片段中显示

[英]Capture Image from Camera/Gallery and Display in Activity/Fragment using Kotlin

i know this is a trivial question well answered using java, but i'm sure there are new APIs to make things easier such as我知道这是一个使用 java 很好回答的微不足道的问题,但我确信有新的APIs可以让事情变得更容易,例如

val getContent = registerForActivityResult(GetContent()) { uri: Uri? -> // Handle the returned Uri }

and

val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) { success: Boolean ->
    if (success) {
        // The image was saved into the given Uri -> do something with it
    }
}

val imageUri: Uri = ...
button.setOnClickListener {
    takePicture.launch(imageUri)
}

Q: How to implement the above question using kotlin and implementing the lastest APIs for that问:如何使用 kotlin 实现上述问题并为此实现最新的APIs

PS: this is question is still valid if answers provided become depreciated or obsolete. PS:如果提供的答案已贬值或过时,这个问题仍然有效。

Here is my code, hope this help这是我的代码,希望对您有所帮助

  • Taking picture:拍照:
  fun takePicture() {
        val root =
               File(Environment.getExternalStorageDirectory(), BuildConfig.APPLICATION_ID + File.separator)
           root.mkdirs()
           val fname = "img_" + System.currentTimeMillis() + ".jpg"
           val sdImageMainDirectory = File(root, fname)
           viewModel.profileImageUri = FileProvider.getUriForFile(requireContext(), context?.applicationContext?.packageName + ".provider", sdImageMainDirectory)
           takePicture.launch(viewModel.profileImageUri)
       }

  val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) { success: Boolean ->
       if (success) {
           // The image was saved into the given Uri -> do something with it
             Picasso.get().load(viewModel.profileImageUri).resize(800,800).into(registerImgAvatar)
       }
   }

  • Select from library库中的 Select
    private val pickImages = registerForActivityResult(ActivityResultContracts.GetContent()){ uri: Uri? ->
            uri?.let { it ->
                // The image was saved into the given Uri -> do something with it
               Picasso.get().load(it).resize(800,800).into(registerImgAvatar)
            }
        }

Then call the function when press button:然后按下按钮时调用 function:

btnSelectFromGallery.setOnClickListener {
                pickImages.launch("image/*")
            }
btnTakePicture.setOnClickListener {
                takePicture()
            }

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

相关问题 从相机捕获图像并在活动中显示 - Capture Image from Camera and Display in Activity 从相机拍摄照片并在另一个活动的图像视图中显示它 - capture a photo from camera and display it in image view in another activity 从相机捕获图像并在同一活动中显示。 -Xamarin - Capture Image from Camera and Display in Same Activity. -Xamarin 从相机或画廊拍摄图像并在活动中显示 - Take image from camera or gallery and show in activity 在片段 Android Sudio Kotlin 的相机/图库中添加 ImageView - adding in ImageView from Camera/Gallery in Fragment Android Sudio Kotlin 如何从相机中捕获图像,片段, - how to capture image from camera, in fragment, 使用Android Studio从相机捕获图像并从Android手机的图库中选择图像 - Capture Image From Camera and Select Image From Gallery of Android Phone Using Android Studio 如何从图库中选择图像或拍摄照片,然后以完整质量显示在另一活动中 - How to select an image from gallery or to capture a photo and then to display it in another activity full quality 从图库/相机中选择图像并显示在 ListView 中 - Choose Image from Gallery/Camera and display in a ListView 如何在图库中的imageview中显示图像? - how to display image in imageview from gallery, in fragment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM