在拍摄照片后,如何启用一个按钮来启动下一个活动?

发布于 2025-01-23 19:54:13 字数 8805 浏览 0 评论 0原文

我想在用户填充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>

I want to activate the button after the user fills the editText and takes the photo, to launch the next activity. I already granted access to the Manifest. XML, and added some code to the activity. The camera opens fine but can't store the picture nor launch the next activity. Please, let me know how I can do this.

Here's my activity:

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()

            }

        }

    }
}

and here's my layout

    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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜扑 2025-01-30 19:54:13

如果我正确理解。您正在使用StartActivityForresult(Intent,Camera_request_code)获取相机照片。但是您不是在听结果。

在这种情况下,StartActivityForresult开放了新的活动,这是用于制作照片的系统活动。拍照后,此活动正在关闭,结果返回到呼叫活动(IDCheck1)。

要读取这些结果,您的活动必须覆盖功能OnActivityResult(requestCode:int,result Code:int,数据,数据:意图?),您可以在哪里决定如何处理获得的数据。

RequestCode参数标识了从哪个活动来的数据。因此,如果您使用Camera_request_code启动ActivityForresult,则需要在结果上收听相同的请求代码。
结果代码是活动如何像activy.result_ok,activity.Result_canceled或其他
数据是从活动示例代码中获取的意图数据

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

If I understand correctly. You are using startActivityForResult(intent, CAMERA_REQUEST_CODE) to get camera photo. But you are not listening for results.

startActivityForResult opens new activity in this case it is system activity for making photos. After taking photo this activity is closing and result are returning to calling activity (idcheck1).

To read those results your activity must override function onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) where you can decide what to do with acquired data.

requestCode parameter identifies from which activity comes data. So if you startedActivityForResult with CAMERA_REQUEST_CODE you need to listen for the same request code on result.
resultCode is status how activity ended like Activity.RESULT_OK, Activity.RESULT_CANCELED or other
data is intent data acquired from activity

Example code:

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.
    }
}

or with newer 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.
    }
}

You can read more about it here https://developer.android.com/training/basics/intents/result

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文