我正在使用camera2并收到以下异常:
04-13 20:39:54.801 13097-13110/com.x.y.z E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'release' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at android.view.Surface.setNativeObjectLocked(Surface.java:460)
at android.view.Surface.<init>(Surface.java:152)
at android.media.ImageReader.nativeGetSurface(Native Method)
at android.media.ImageReader.<init>(ImageReader.java:130)
at android.media.ImageReader.newInstance(ImageReader.java:100)
at androidx.camera.core.MetadataImageReader.createImageReaderProxy(MetadataImageReader.java:124)
at androidx.camera.core.MetadataImageReader.<init>(MetadataImageReader.java:113)
at androidx.camera.core.ImageCapture.createPipeline(ImageCapture.java:335)
at androidx.camera.core.ImageCapture.onSuggestedResolutionUpdated(ImageCapture.java:974)
at androidx.camera.core.UseCase.updateSuggestedResolution(UseCase.java:372)
at androidx.camera.core.CameraX.bindToLifecycle(CameraX.java:321)
at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:229)
at com.x.y.z.fragments.CameraFragment$bindCameraUseCases$1.run(CameraFragment.kt:286)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5651)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
查看堆栈跟踪信息表明它来自SurfaceView,尚未发布。检查参考资料表明,在Java中,您必须释放它;不太确定如何在Kotlin中处理它。
顺便说一句,我已经对CameraXBasic进行了很小的修改,因此在触摸图库按钮时它将运行以下代码:
// Listener for button used to view the most recent photo
camControls.findViewById<ImageButton>(R.id.photo_view_button).setOnClickListener {
pickFromGallery()
}
private fun pickFromGallery() {
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
val mimeTypes =
arrayOf("image/jpeg", "image/png")
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
startActivityForResult(intent, GALLERY_REQUEST_CODE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK && requestCode == GALLERY_REQUEST_CODE){
val selectedUri = data?.data
val filePathColumn = MediaStore.Images.Media.DATA
val cursor = context?.contentResolver?.query(selectedUri!!, arrayOf(filePathColumn), null, null, null)
cursor?.moveToFirst()
val columnIndex = cursor?.getColumnIndex(filePathColumn)
val absolutePath = cursor?.getString(columnIndex!!)
cursor?.close()
if(absolutePath != null) setImagePathAndPopBack(File(absolutePath))
}
}
此代码正确显示了选择图片库的选项,但是如果我只是在外面触摸以将其关闭,则应用程序会因上述异常而崩溃。要重现它的肯定方法是触摸画廊图标,然后将其关闭几次,而无需打开任何照片画廊。为了清楚起见,大多数onActivityResult代码都没有执行,因为我什至没有选择任何图像。
build.gradle:
implementation "androidx.camera:camera-core:1.0.0-beta02"
implementation "androidx.camera:camera-camera2:1.0.0-beta02"
implementation "androidx.camera:camera-lifecycle:1.0.0-beta02"
implementation "androidx.camera:camera-view:1.0.0-alpha09"
Android Studio:3.6.2Kotlin:1.3.61
提前感谢您的帮助!
编辑1:
我意识到这仅在我使用StrictMode时发生
StrictMode.setVmPolicy(
StrictMode.VmPolicy.Builder()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build()
)
如果我不带刑罚而运行我的代码,那么它的工作原理与示例一样流畅。
我发现解决方法是避免因泄漏可关闭的物体而招致死刑。看来,该错误来自Android API,我已将其指出给Android开发人员,他们选择不对其采取行动。