Fix photo error reporting and add detailed logging

This commit is contained in:
xiajiid
2026-02-08 22:50:56 +08:00
parent f1a7811d28
commit 1a45fde858

View File

@@ -43,6 +43,11 @@ class SimpleCameraActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_simple_camera) setContentView(R.layout.activity_simple_camera)
// 记录应用启动信息
Log.d(TAG, "SimpleCameraActivity 启动")
Log.d(TAG, "Android SDK: ${Build.VERSION.SDK_INT}")
Log.d(TAG, "设备型号: ${Build.MODEL}")
// 初始化视图 // 初始化视图
viewFinder = findViewById(R.id.viewFinder) viewFinder = findViewById(R.id.viewFinder)
captureButton = findViewById(R.id.captureButton) captureButton = findViewById(R.id.captureButton)
@@ -52,15 +57,20 @@ class SimpleCameraActivity : AppCompatActivity() {
// 请求权限 // 请求权限
if (allPermissionsGranted()) { if (allPermissionsGranted()) {
Log.d(TAG, "权限已授予,启动相机")
startCamera() startCamera()
} else { } else {
Log.d(TAG, "请求相机权限")
ActivityCompat.requestPermissions( ActivityCompat.requestPermissions(
this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS
) )
} }
// 设置拍照按钮点击监听器 // 设置拍照按钮点击监听器
captureButton.setOnClickListener { takePhoto() } captureButton.setOnClickListener {
Log.d(TAG, "拍照按钮被点击")
takePhoto()
}
} }
override fun onDestroy() { override fun onDestroy() {
@@ -139,20 +149,48 @@ class SimpleCameraActivity : AppCompatActivity() {
object : ImageCapture.OnImageSavedCallback { object : ImageCapture.OnImageSavedCallback {
override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) { override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
val savedUri = outputFileResults.savedUri val savedUri = outputFileResults.savedUri
val msg = if (savedUri != null) { if (savedUri != null) {
"照片已保存: ${savedUri.lastPathSegment}" val msg = "照片已保存: ${savedUri.lastPathSegment}"
} else {
"照片保存成功"
}
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show() Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
Log.d(TAG, msg) Log.d(TAG, msg)
// 验证文件是否真的存在
try {
val cursor = contentResolver.query(savedUri, null, null, null, null)
if (cursor != null && cursor.moveToFirst()) {
Log.d(TAG, "文件验证成功: ${savedUri}")
} else {
Log.w(TAG, "文件验证失败但onImageSaved被调用: ${savedUri}")
}
cursor?.close()
} catch (e: Exception) {
Log.e(TAG, "文件验证异常: ${e.message}")
}
} else {
Log.w(TAG, "onImageSaved被调用但savedUri为null")
Toast.makeText(baseContext, "照片保存完成", Toast.LENGTH_SHORT).show()
}
} }
override fun onError(exception: ImageCaptureException) { override fun onError(exception: ImageCaptureException) {
Log.e(TAG, "拍照失败: ${exception.message}", exception) val errorMsg = "拍照失败: ${exception.message}"
Log.e(TAG, errorMsg, exception)
// 检查错误类型
when (exception.imageCaptureError) {
ImageCapture.ERROR_CAMERA_CLOSED ->
Toast.makeText(baseContext, "相机已关闭", Toast.LENGTH_LONG).show()
ImageCapture.ERROR_FILE_IO ->
Toast.makeText(baseContext, "文件保存失败", Toast.LENGTH_LONG).show()
ImageCapture.ERROR_INVALID_CAMERA ->
Toast.makeText(baseContext, "无效的相机", Toast.LENGTH_LONG).show()
ImageCapture.ERROR_UNKNOWN ->
Toast.makeText(baseContext, "未知错误", Toast.LENGTH_LONG).show()
else ->
Toast.makeText(baseContext, "拍照失败: ${exception.message}", Toast.LENGTH_LONG).show() Toast.makeText(baseContext, "拍照失败: ${exception.message}", Toast.LENGTH_LONG).show()
} }
} }
}
) )
} }