【问题标题】:how to update ImageView from bitmap in android如何从android中的位图更新ImageView
【发布时间】:2023-01-18 05:24:25
【问题描述】:

我正在通过本地服务器的套接字接收图像的 byteArray。 byteArray 表示为 RGB。我正在将它转换为位图。我检查了位图不为空。但是当我使用 ImageView.setImageBitmap(bitmap) 然后我的 android 应用程序崩溃。我已经尝试了很多东西,但还找不到解决方案。请帮帮我

class Cam : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d("Cam activity", "onCreate")
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_cam)

        val button_back : Button = findViewById(R.id.button_back)!!
        var textView : TextView = findViewById(R.id.textView)!!
        var imageView: ImageView = findViewById(R.id.Image_update)!!

        Thread {
            Thread.sleep(1000)
            var byteArray = ByteArray(76032)
            Connection.inBytesCam?.read(byteArray)
            var nrOfPixels = byteArray.size / 3 // Three bytes per pixel.
            var pixels = IntArray(nrOfPixels)
            for (i in 0 until nrOfPixels){
                var r = byteArray.get(3*i).toInt()
                var g = byteArray.get(3*i + 1).toInt()
                var b = byteArray.get(3*i + 2).toInt()
                pixels.set(i, Color.rgb(r, g, b))
            }
            var bitmap = Bitmap.createBitmap(pixels, 176, 144, Bitmap.Config.ARGB_8888)
            imageView.setImageBitmap(bitmap)

        }.start()

        button_back.setOnClickListener {
            Thread {
                runOnUiThread {
                    val intent = Intent(this@Cam, ControlActivity::class.java)
                    startActivity(intent)
                }
            }.start()
        }

    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/purple_200"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="-103dp">

    <Button
        android:id="@+id/button_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/purple_700"
        android:fontFamily="monospace"
        android:text="Back"
        android:textAlignment="center"
        android:textSize="34sp"
        app:iconTint="@color/purple_700"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.862" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="246dp"
        android:layout_height="135dp"
        android:layout_marginBottom="52dp"
        android:text="LOG"
        android:textAlignment="textStart"
        app:layout_constraintBottom_toTopOf="@+id/button_back"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintStart_toStartOf="parent" />

    <ImageView
        android:id="@+id/Image_update"
        android:layout_width="176dp"
        android:layout_height="144dp"
        android:layout_marginTop="100dp"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@drawable/camera_recording" />

</androidx.constraintlayout.widget.ConstraintLayout>

我使用 DataInputStream/DataOutputStream 将 ImageBytes 从本地服务器传输到 android 上的客户端。 我找不到任何能解决我问题的东西

一般来说,我需要循环更新图像,比如视频播放器。但我什至无法更新一张图片:(

【问题讨论】:

    标签: android arrays kotlin android-layout bitmap


    【解决方案1】:

    使用 Glide 库如下

    添加依赖;

    implementation 'com.github.bumptech.glide:glide:4.11.0'
    

    在您的活动等中使用

    Glide.with(context)
        .load(bitmap)
        .into(imageView)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多