gpt4 book ai didi

android - 拍照,旋转屏幕并返回 Activity 后,应用突然崩溃

转载 作者:行者123 更新时间:2023-12-02 13:36:46 24 4
gpt4 key购买 nike

拍照,旋转屏幕并返回 Activity 后,App突然崩溃。
我正在制作一个需要拍照并将其路线存储为String的数据库的Android应用,我遵循了Google官方教程(https://developer.android.com/training/camera/photobasics#kotlin)的操作,而无需实现自己的相机应用。
但是当我执行以下步骤时遇到问题:
•使用媒体dispatchTakePictureIntent()打开包含用于拍照的逻辑的 Activity
•以人像模式启动相机。
•将屏幕旋转至水平/相机至风景模式。
•拍摄照片,然后点击“确定”按钮。
•应用程序崩溃。

但是,如果在相机打开时不旋转手机,我可以根据需要使用捕获的照片。

我的代码是这样的:

   override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(requestCode == CodigosDeSolicitud.ANADIR_FOTOGRAFIA && resultCode == Activity.RESULT_OK){
setPic()
}
}

@Throws(IOException::class)
private fun createImageFile() : File{

val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val storageDir : File = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile(
"JPEG_${timeStamp}_",
".jpg",
storageDir
).apply {
mCurrentPhotoPath = absolutePath
}

}

private fun dispatchTakePicktureIntent(){
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also {takePictureIntent ->
takePictureIntent.resolveActivity(packageManager)?.also {
val photoFile : File? = try{
createImageFile()
} catch (ex : IOException){
null
}
photoFile?.also {
val photoURI: Uri = FileProvider.getUriForFile(
this,
"com.kps.spart.android.fileprovider",
it
)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(takePictureIntent, CodigosDeSolicitud.ANADIR_FOTOGRAFIA)
}
}

}
}

private fun setPic(){
val targetW: Int = iconoUsuarioIV.width
val targetH: Int = iconoUsuarioIV.height

val bmOptions = BitmapFactory.Options().apply {
inJustDecodeBounds = true
BitmapFactory.decodeFile(mCurrentPhotoPath,this)
val photoW: Int = outWidth
val photoH: Int = outHeight

val scaleFactor : Int = Math.min(photoW / targetW, photoH / targetH)

inJustDecodeBounds = false
inSampleSize = scaleFactor
inPurgeable = true
}

val exif = ExifInterface(mCurrentPhotoPath)
val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_UNDEFINED)
Toast.makeText(this@RegistrarUsuarioActivity,"Orientacion: " + orientation, Toast.LENGTH_SHORT).show()

BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions)?.also { bitmap ->
iconoUsuarioIV.setImageBitmap(bitmap)
}

我的logcat给我下一个错误:
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1000, result=-1, data=null} to activity {com.kps.spart.moskimedicationreminder/com.kps.spart.moskimedicationreminder.RegistrarUsuarioActivity}: java.lang.ArithmeticException: divide by zero
at android.app.ActivityThread.deliverResults(ActivityThread.java:4519)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3777)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3845) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3065) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4950) 
at android.app.ActivityThread.-wrap19(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1730) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:7000) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) 
Caused by: java.lang.ArithmeticException: divide by zero
at com.kps.spart.moskimedicationreminder.RegistrarUsuarioActivity.setPic(RegistrarUsuarioActivity.kt:223)
at com.kps.spart.moskimedicationreminder.RegistrarUsuarioActivity.onActivityResult(RegistrarUsuarioActivity.kt:170)
at android.app.Activity.dispatchActivityResult(Activity.java:7630)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4515)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3777) 
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3845) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3065) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4950) 
at android.app.ActivityThread.-wrap19(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1730) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:7000) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) 

使用中断点,我发现在拍照时旋转屏幕会破坏 Activity 。但是,如果我不在 Activity 中,为什么会发生这种情况,我该如何解决此问题?

最佳答案

我本人偶然发现了这个(遵循相同的指南)。我知道这个问题很旧,但是我正在发布其他可能遇到的解决方案。

问题出在两次调用的onCreate方法中(拍照后,设备旋转后返回 Activity 时)。为了避免这种情况,您应该阻止第二次调用dispatchTakePicktureIntent(我猜您在onCreate方法中这样做了)。

我已经这样实现了:

class TakePhotoActivity : AppCompatActivity() {

private val captureRequestCode = 1
private val captureState = "capture_in_progress"
private val capturePhotoPath = "capture_photo_path"
private var photoPath: String? = null
private var captureInProgress: Boolean = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_take_photo)
captureInProgress = savedInstanceState?.getBoolean(captureState) ?: false
photoPath = savedInstanceState?.getString(capturePhotoPath)

if(!captureInProgress){
captureInProgress = true
dispatchTakePictureIntent()
}
}

override fun onSaveInstanceState(outState: Bundle) {
outState.putBoolean(captureState, captureInProgress)
outState.putString(capturePhotoPath, photoPath)
super.onSaveInstanceState(outState)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == captureRequestCode && resultCode == RESULT_OK) {
user_photo.adjustViewBounds = true
val imgFile = File(photoPath ?: "")

if (imgFile.exists()) {
val myBitmap = BitmapFactory.decodeFile(imgFile.absolutePath)
user_photo.setImageBitmap(myBitmap)
runImageLabelingOnCurrentPicture(myBitmap)
}
captureInProgress = false
}
}
}

您需要将“正在处理图片”标记保存到 savedInstanceState中,如果 onCreate方法中存在该标记,则将其还原。就这些。

关于android - 拍照,旋转屏幕并返回 Activity 后,应用突然崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54497452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com