【问题标题】:Convert image from gallery to bitmap. I get a NullPointerException将图像从画廊转换为位图。我得到一个 NullPointerException
【发布时间】:2023-01-07 02:58:07
【问题描述】:

我正在尝试将图库中的图像转换为 bitmp,然后将其存储在数组中。但是,以前,我收到 NullPointerException。 如何转换图库中的图像?

添加新主页片段.kt:

...

else if(requestCode == GALLERY &&  resultCode == Activity.RESULT_OK && data != null)
    {
        //imgData = "content://media/external/images/media/100051...
        val imgData = data.data!!
        val inputStream = requireContext().contentResolver.openInputStream(imgData)

        val exif = ExifInterface(inputStream!!)

        val rotation = exif.getAttributeInt(
            ExifInterface.TAG_ORIENTATION,
            ExifInterface.ORIENTATION_UNDEFINED
        )
        val rotationInDegrees: Int = exifToDegrees(rotation)

        //ERROR THROWS HERE
        val bitmap = BitmapFactory.decodeStream(inputStream)

        val bitmapReturn = rotateBitmap(bitmap,rotationInDegrees)
        listImg[aktuellesBild] = bitmapReturn!!

        adapter.notifyItemChanged(aktuellesBild)
    }

Caused by: java.lang.NullPointerException: 位图不能为空

【问题讨论】:

    标签: android android-studio kotlin android-imageview android-bitmap


    【解决方案1】:

    您不能重复使用 inputStream。 ExifInterface 已经消耗了流。您需要再次致电openInputStream() 以获得新的InputStream 以传递给BitmapFactory.decodeStream()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      相关资源
      最近更新 更多