如何使按钮在拍照后启动下一个活动?



我想在用户填写editText并拍摄照片后激活按钮,以启动下一个活动。我已经批准了进入舱单的权限。XML,并向该活动添加了一些代码。相机打开很好,但不能存储照片,也不能启动下一个活动。请让我知道我该怎么做。

这是我的活动:

package com.example.myapplication
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_idcheck.*
class idcheck1 : AppCompatActivity() {
companion object {
private const val CAMERA_PERMISSION_CODE = 1
private const val CAMERA_REQUEST_CODE = 2
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_idcheck)
findViewById<Button>(R.id.x)
x.setOnClickListener {
val intent = Intent(this, Getstarted::class.java)
startActivity(intent)
}
findViewById<Button>(R.id.verificame)
verificame.setOnClickListener {
val intent = Intent(this, TYCacreedores::class.java)
startActivity(intent)
}
upload.setOnClickListener {
if (ContextCompat.checkSelfPermission(
this,
android.Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED
) {
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(intent, CAMERA_REQUEST_CODE)
} else {
ActivityCompat.requestPermissions(
this,
arrayOf(android.Manifest.permission.CAMERA),
CAMERA_PERMISSION_CODE
)

}

}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == CAMERA_PERMISSION_CODE) {
if (grantResults.isNotEmpty() && grantResults[0] === PackageManager.PERMISSION_GRANTED) {
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(intent, CAMERA_REQUEST_CODE)
} else {
Toast.makeText(
this,
"Bueeno, negaste el permiso para acceder a tu cámara. " +
" No te apures, permitelo en configuraciones en tu teléfono.",
Toast.LENGTH_LONG
).show()
}
}
}
}

这是我的布局

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:openDrawer="start"
android:background="@color/Darkest"
android:orientation="vertical"
tools:context=".idcheck1">

<ScrollView
android:layout_width="match_parent"
android:layout_height="563dp"
android:columnCount="1"
android:columnOrderPreserved="false"
android:rowCount="1">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@drawable/rounded1"
android:columnCount="1"
android:columnOrderPreserved="false"
android:rowCount="1">
<ImageView
android:id="@+id/x"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="320dp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
android:src="@drawable/close_circle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:fontFamily="@font/poppinsbold"
android:gravity="center"
android:text="Tú identidad."
android:textColor="@color/colorPrimary"
android:textSize="30sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:fontFamily="@font/poppins_semibold"
android:text="¿Cúal es tu cédula?"
android:textColor="@color/colorPrimary"
android:textSize="18sp" />

<EditText
android:id="@+id/correo"
android:layout_width="281dp"
android:layout_height="40dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:background="@drawable/rounded3"
android:ems="10"
android:fontFamily="@font/poppinsregular"
android:hint="03700000001"
android:inputType="number"
android:maxLength="11"
android:paddingLeft="20dp"
android:selectAllOnFocus="true"
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorDark"
android:textSize="18sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:fontFamily="@font/poppins_semibold"
android:text="Sube pruebas."
android:textColor="@color/colorPrimary"
android:textSize="18sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:fontFamily="@font/poppinsregular"
android:text="*Cédula de ambos lados."
android:textColor="@color/colorPrimary"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:fontFamily="@font/poppinsregular"
android:text="*Foto sosteniendo tú cédula."
android:textColor="@color/colorPrimary"
android:textSize="16sp" />

<ImageView
android:id="@+id/upload"
android:layout_width="174dp"
android:layout_height="83dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:src="@drawable/upload" />

<Button
android:id="@+id/verificame"
android:layout_width="247dp"
android:layout_height="59dp"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/roundedbutton3"
android:clickable="true"
android:focusable="true"
android:fontFamily="@font/poppins_semibold"
android:foreground="?attr/selectableItemBackground"
android:text="Verificame!"
android:textAllCaps="false"
android:textColor="@color/colorDark"
android:textSize="20sp" />
</GridLayout>
</ScrollView>
</LinearLayout>

如果我理解正确的话。你正在使用startActivityForResult(intent, CAMERA_REQUEST_CODE)来获取相机照片。但是你没有在听结果。

startActivityForResult打开新活动,在本例中是用于制作照片的系统活动。拍照后,这个活动关闭,结果返回到调用活动(idcheck1)。

要读取这些结果,您的活动必须覆盖函数onActivityResult(requestCode: Int, resultCode: Int, data: Intent?),在那里您可以决定如何处理获取的数据。

requestCode参数标识来自哪个活动的数据。所以如果你用CAMERA_REQUEST_CODE来starttedactivityforresult,你需要在result上监听相同的请求代码。
resultCode是activity结束的状态。RESULT_OK,活动。result_cancelled或其他数据是从活动中获取的意图数据

示例代码:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
val imageBitmap = data?.extras?.get("data") as Bitmap //get Photo Image

//here you can add aditional logic after making photo. Start another activity/enable button/save image/check EditText etc.
}
}

或使用较新的api

resultLauncher.launch(intent); // replace  startActivityForResult(intent, CAMERA_REQUEST_CODE)
var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val intent = result.data
val imageBitmap = intent?.extras?.get("data") as Bitmap
//here you can add aditional logic after making photo. Start another activity/enable button/save image etc.
}
}

你可以在这里阅读更多关于https://developer.android.com/training/basics/intents/result

最新更新