应用程序在横向模式下接受照片时崩溃

Application Crashes Accepting Photo In Landscape Mode

我有一个使用相机意图的 activity。如果我垂直拍摄照片并接受在图像视图中垂直显示它,则没有问题。如果我水平拍摄一张照片并在水平放置手机时接受它,它会崩溃,但如果我将它转为垂直,它就会工作。有人知道为什么会这样吗? 它没有多大意义,因为当应用程序崩溃时,它唯一说的是 photoFile 为空,似乎没有图片但实际上有图片。 这是来自 activity:

的代码
private const val FILE_NAME = "photo.jpg"

class TextCameraActivity : AppCompatActivity(), TextToSpeech.OnInitListener {

private lateinit var binding: ActivityTextCameraBinding

private lateinit var bitmap : Bitmap

private lateinit var photoFile: File

private var tts: TextToSpeech? = null
private var locale : Locale = Locale("es", "ES")

private var progressBar : ProgressBar? = null
private var i = 0
private val handler = Handler()


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityTextCameraBinding.inflate(layoutInflater)
    setContentView(binding.root)

    binding.btn7.setOnClickListener {
        val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        photoFile = getPhotoFile(FILE_NAME)

        val fileProvider = FileProvider.getUriForFile(this, "com.example.fileprovider", photoFile)
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileProvider)

        if(takePictureIntent.resolveActivity(this.packageManager) != null) {
            startActivityForResult(takePictureIntent, 42)

        } else {
            Toast.makeText(this, "Unable to open camera", Toast.LENGTH_SHORT).show()
            Log.d("mensaje", "?????Unable to open camera")
        }
    }

    tts = TextToSpeech(this, this)

    progressBar = binding.progressBar

    binding.btnReconocerImagen.setOnClickListener {

        progressBar!!.visibility = View.VISIBLE
        i = progressBar!!.progress

        Thread(Runnable {
            while (i < 10) {
                i += 1
                handler.post(Runnable {
                    progressBar!!.progress = i

                })
                try {
                    Thread.sleep(100)
                } catch (e: InterruptedException) {
                    e.printStackTrace()
                }
            }
            progressBar!!.visibility = View.INVISIBLE

        }).start()
        recognizeText()
    }

    val actionBar = supportActionBar
    actionBar?.setDisplayHomeAsUpEnabled(true)
}

private fun getPhotoFile(fileName:String):File {
    val storageDirectory = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
    return File.createTempFile(fileName, ".jpg", storageDirectory)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.getItemId()) {
        android.R.id.home -> {
            finish()
            return true
        }
    }
    return super.onOptionsItemSelected(item)
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    return true
}

override fun onInit(status: Int) {

    if (status == TextToSpeech.SUCCESS) {
        // set US English as language for tts
        val result = tts!!.setLanguage(locale)

        if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS","The Language specified is not supported!")
        } else {
            binding.btnReconocerImagen.isEnabled = true

        }

    } else {
        Log.e("TTS", "Initilization Failed!")
    }

}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if(requestCode == 42 && resultCode == Activity.RESULT_OK) {
        bitmap = BitmapFactory.decodeFile(photoFile.absolutePath)
        binding.imageView.setImageBitmap(bitmap)
    } else {
        super.onActivityResult(requestCode, resultCode, data)
    }

}

private fun recognizeText() {

    try {
        val image = InputImage.fromBitmap(bitmap, 0)

        val recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)

        val result = recognizer.process(image)
            .addOnSuccessListener { visionText ->

                for (block in visionText.textBlocks) {
                    val boundingBox = block.boundingBox
                    val cornerPoints = block.cornerPoints
                    val text = block.text

                    Log.d("mensaje", "he encontrado $text")
                    binding.tvTextoReconocido.text = "El texto reconocido es: $text"
                    tts!!.speak("El texto reconocido es $text", TextToSpeech.QUEUE_FLUSH, null, "")

                    for (line in block.lines) {
                        // ...
                        for (element in line.elements) {
                            // ...
                        }
                    }
                }
            }
            .addOnFailureListener { e ->

            }


    } catch (e : Exception) {
        Log.d("mensaje", "NO HAY IMAGEN SELECCIONADA")
        Toast.makeText(this,"NO HAS SELECCIONADO NINGUNA IMAGEN PARA RECONOCER", Toast.LENGTH_SHORT).show()
    }


}

}

错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.appdigitalinkrecognition, PID: 30407
    java.lang.RuntimeException: Unable to resume activity {com.example.appdigitalinkrecognition/com.example.appdigitalinkrecognition.TextCameraActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=42, result=-1, data=null} to activity {com.example.appdigitalinkrecognition/com.example.appdigitalinkrecognition.TextCameraActivity}: kotlin.UninitializedPropertyAccessException: lateinit property photoFile has not been initialized
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4918)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4955)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2336)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8653)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
     Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=42, result=-1, data=null} to activity {com.example.appdigitalinkrecognition/com.example.appdigitalinkrecognition.TextCameraActivity}: kotlin.UninitializedPropertyAccessException: lateinit property photoFile has not been initialized
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5590)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4905)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4955) 
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52) 
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2336) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:246) 
        at android.app.ActivityThread.main(ActivityThread.java:8653) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 
     Caused by: kotlin.UninitializedPropertyAccessException: lateinit property photoFile has not been initialized
        at com.example.appdigitalinkrecognition.TextCameraActivity.onActivityResult(TextCameraActivity.kt:143)
        at android.app.Activity.dispatchActivityResult(Activity.java:8550)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5583)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4905) 
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4955) 
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52) 
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2336) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:246) 
        at android.app.ActivityThread.main(ActivityThread.java:8653) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
```` 

photoFile 声明为可选应该可以解决问题:

private var photoFile: File? = null 

您需要在 onActivityResult 中添加 null-check:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if(requestCode == 42 && resultCode == Activity.RESULT_OK) {
        photoFile?.let { photo -> 
            bitmap = BitmapFactory.decodeFile(photo.absolutePath)
            binding.imageView.setImageBitmap(bitmap)
        } ?: run {
            // Photo is not defined...
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data)
    }
}

如果我不得不猜测,您会得到不同的结果,因为更改方向会破坏 Activity,因此可以创建一个新的以匹配新的方向。所以也许这发生在背景中 - 但如果你垂直拍摄照片,方向不会改变,所以 Activity 不会被破坏

这是个问题,因为 like the docs say:

Note: Since your process and activity can be destroyed between when you call launch() and when the onActivityResult() callback is triggered, any additional state needed to handle the result must be saved and restored separately from these APIs.

这是我能找到的与他们最接近的明确说法“onActivityResult 可以在 onCreate 之前 运行”,但如果这就是你正在发生的事情,那么这就是你的原因重新收到 lateinit property photoFile has not been initialized 错误。

可能也是因为你实际上并没有在 onCreate 中初始化它,你是在点击监听器中进行初始化 - 所以即使 onCreate 运行 首先,按钮需要再次点击,onActivityResult 肯定在 运行 之前。但仅仅将其移动到 onCreate 可能还不够


我认为要正确处理此问题,您需要考虑将“显示照片”任务分为两部分 - 创建路径(在 onCreate 中)和获得结果(通过 onActivityResult).你不能保证这两件事的顺序,所以当你做一件的时候,你必须检查另一部分是否已经完成 - 如果已经完成,你可以显示图像。

你可以这样做:

private var pathInitialised = false
private var photoTaken = false

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    // don't do this in the click listener - it needs to be available when the activity starts
    photoFile = getPhotoFile(FILE_NAME)
    // set the flag, and try to show the pic if ready
    pathInitialised = true
    tryDisplayPhoto()
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if(requestCode == 42 && resultCode == Activity.RESULT_OK) {
        // set the flag, and try to show the pic if ready
        photoTaken = true
        tryDisplayPhoto()
    } else {
        super.onActivityResult(requestCode, resultCode, data)
    }
}

private fun tryDisplayPhoto() {
    if (pathInitialised && photoTaken) {
        // now you know that the path is available and a photo needs to be shown
        bitmap = BitmapFactory.decodeFile(photoFile.absolutePath)
        binding.imageView.setImageBitmap(bitmap)
    }
}

所以现在,无论这两个方法的调用顺序如何,它们都会尝试显示图片 - 只有当最后一个方法被调用并且所有部分都已就位时才会发生这种情况