Reputation: 1197
I am getting this error on this function, I am new to Kotlin programming and not sure how I would fix this. the only answewrs I can find on this are for Java and im not sure how i can convert them
I dont know how to make this static or try and take it out of a nested class any help would be appretiated
This AsyncTask class should be static or leaks might occur (anonymous android.os.AsyncTask) less... (⌘F1) A static field will leak contexts.
private fun detectAndFrame(imageBitmap: Bitmap) {
val outputStream = ByteArrayOutputStream()
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
val inputStream = ByteArrayInputStream(outputStream.toByteArray())
val detectTask = object : AsyncTask<InputStream, String, Array<Face>>() {
var exceptionMessage = ""
override fun doInBackground(vararg params: InputStream): Array<Face>? {
try {
publishProgress("Detecting...")
val result = faceServiceClient.detect(
params[0],
true, // returnFaceId
false, null// returnFaceAttributes:
/* new FaceServiceClient.FaceAttributeType[] {
FaceServiceClient.FaceAttributeType.Age,
FaceServiceClient.FaceAttributeType.Gender }
*/
)// returnFaceLandmarks
if (result == null) {
publishProgress(
"Detection Finished. Nothing detected"
)
return null
}
publishProgress(
String.format(
"Detection Finished. %d face(s) detected",
result.size
)
)
return result
} catch (e: Exception) {
exceptionMessage = String.format(
"Detection failed: %s", e.message
)
return null
}
}
override fun onPreExecute() {
//TODO: show progress dialog
detectionProgressDialog.show()
}
override fun onProgressUpdate(vararg progress: String) {
//TODO: update progress
detectionProgressDialog.setMessage(progress[0])
}
override fun onPostExecute(result: Array<Face>) {
//TODO: update face frames
detectionProgressDialog.dismiss()
if (exceptionMessage != "") {
showError(exceptionMessage)
}
if (result == null) return
imageTook.setImageBitmap(
drawFaceRectanglesOnBitmap(imageBitmap, result)
)
imageBitmap.recycle()
}
}
detectTask.execute(inputStream)
}
private fun drawFaceRectanglesOnBitmap(
originalBitmap: Bitmap, faces: Array<Face>?
): Bitmap {
val bitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true)
val canvas = Canvas(bitmap)
val paint = Paint()
paint.isAntiAlias = true
paint.style = Paint.Style.STROKE
paint.color = Color.RED
paint.strokeWidth = 10f
if (faces != null) {
for (face in faces) {
val faceRectangle = face.faceRectangle
canvas.drawRect(
faceRectangle.left.toFloat(),
faceRectangle.top.toFloat(),
(faceRectangle.left + faceRectangle.width).toFloat(),
faceRectangle.top + faceRectangle.height.toFloat(),
paint
)
}
}
return bitmap
}
private fun showError(message: String) {
AlertDialog.Builder(this)
.setTitle("Error")
.setMessage(message)
.setPositiveButton("OK") { dialog, id -> }
.create().show()
}
Upvotes: 0
Views: 512
Reputation: 33
try doing:
class A {
companion object {
class MyAsyncTask: AsyncTask <String?, Void,Intent>(){
protected fun doInBackground(vararg params: String): Intent {
// do stuff
}
override fun onPostExecute(intent: Intent) {
//do stuff
}
}.execute()
}
fun doStuff() {
MyAsyncTask.execute()
}
}
Upvotes: 2