本文介绍了已弃用的"getBitmap"使用API​​29.是否还有其他代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的onActivityResult无法正常工作,因为已弃用getBitmap,是否有其他替代代码可以实现这一目标?

My onActivityResult is not working because getBitmap is deprecated, any alternative codes to achieve this?

这是需要更改的代码,有什么建议吗?

here are the codes that needs to be changed, any suggestions?

val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, selectedPhotoUri)

getBitmap被删除并表示已弃用

推荐答案

这对我有用,

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if(requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {

            val selectedPhotoUri = data.data
            try {
                selectedPhotoUri?.let {
                    if(Build.VERSION.SDK_INT < 28) {
                        val bitmap = MediaStore.Images.Media.getBitmap(
                            this.contentResolver,
                            selectedPhotoUri
                        )
                        imageView.setImageBitmap(bitmap)
                    } else {
                        val source = ImageDecoder.createSource(this.contentResolver, selectedPhotoUri)
                        val bitmap = ImageDecoder.decodeBitmap(source)
                        imageView.setImageBitmap(bitmap)
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

这篇关于已弃用的"getBitmap"使用API​​29.是否还有其他代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 17:11