من هذا الكود package com.test.photoeditor

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {


    private lateinit var originalImage: Bitmap
    private lateinit var imageView: ImageView
    private lateinit var maskView: View

    private lateinit var result: Bitmap

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        originalImage = BitmapFactory.decodeResource(resources, R.drawable.cat)
        imageView = findViewById(R.id.imageView)
        imageView.setImageBitmap(originalImage)

        maskView = findViewById(R.id.maskView)
        maskView.setOnTouchListener(object : View.OnTouchListener {
            override fun onTouch(v: View, event: MotionEvent): Boolean {
                val x = event.x
                val y = event.y

                // Convert the touch coordinates to the coordinates of the mask image.
                val maskX = (x - maskView.left) / maskView.width
                val maskY = (y - maskView.top) / maskView.height

                // Update the mask image.
                val maskBitmap = Bitmap.createBitmap(
                    originalImage.width,
                    originalImage.height,
                    Bitmap.Config.ARGB_8888
                )
                val canvas = Canvas(maskBitmap)
                val paint = Paint()
                paint.color = Color.WHITE
                canvas.drawRect(
                    0f,
                    0f,
                    originalImage.width.toFloat(),
                    originalImage.height.toFloat(),
                    paint
                )
                paint.color = Color.BLACK
                canvas.drawRect(
                    maskX * originalImage.width,
                    maskY * originalImage.height,
                    (maskX + 1) * originalImage.width,
                    (maskY + 1) * originalImage.height,
                    paint
                )

                // Initialize the result bitmap.
                result = Bitmap.createBitmap(
                    originalImage.width,
                    originalImage.height,
                    Bitmap.Config.ARGB_8888
                )

                // Resize the mask bitmap to the same size as the result bitmap.
                val resizedMaskBitmap =
                    Bitmap.createScaledBitmap(maskBitmap, result.width, result.height, true)

                // Update the image view.
                imageView.setImageBitmap(
                    removeBackground(
                        originalImage,
                        resizedMaskBitmap
                    ) as Bitmap
                )

                return true
            }
        })
    }

    private fun removeBackground(image: Bitmap, mask: Bitmap): Bitmap {
        val canvas = Canvas(result)
        val paint = Paint().apply {
            xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN)
        }
        canvas.drawBitmap(image, 0f, 0f, null)
        canvas.drawBitmap(mask, 0f, 0f, paint)
        paint.xfermode = null
        paint.color = Color.TRANSPARENT
        return result
    }
} يحدث هذا الخطا 2023-05-23 21:13:58.724 11834-11834 MirrorManager           com.test.photoeditor                 W  this model don't Support
2023-05-23 21:14:16.284 11834-14741 est.photoeditor         com.test.photoeditor                 I  Background concurrent copying GC freed 366895(15MB) AllocSpace objects, 0(0B) LOS objects, 49% free, 3427KB/6854KB, paused 239us,68us total 101.975ms
2023-05-23 21:14:16.303 11834-11834 FinalizerDaemon         com.test.photoeditor                 W  type=1400 audit(0.0:303790): avc: denied { getopt } for path="/dev/socket/usap_pool_primary" scontext=u:r:untrusted_app:s0:c21,c261,c512,c768 tcontext=u:r:zygote:s0 tclass=unix_stream_socket permissive=0
2023-05-23 21:14:16.313 11834-14743 System                  com.test.photoeditor                 W  A resource failed to call close.  ما الحل