简体   繁体   中英

how to update ImageView from bitmap in android

I am receiving a byteArray of my image over a socket from a localserver. the byteArray is represented as RGB. I'm converting it to bitmap. I checked that the bitmap is not empty. but when I use ImageView.setImageBitmap(bitmap) then my android app crashes. I've already tried a lot of things and I can't find a solution yet. help me please

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>

i use DataInputStream/DataOutputStream to transfer ImageBytes from localserver to client on android. i cant find anything that solve my problem

in general, I need to have an image update in a loop, like video player. but I can't even update one image:(

Use Glide library as below

add dependency;

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

use in your activity etc.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM