如何在 Android 中以编程方式点击相机?
kotlin androidapps/applicationsmobile development更新于 2026/2/15 9:22:17
此示例演示了如何
步骤 1 − 在 Android Studio 中创建一个新项目,转到"文件">"新建项目",并填写所有必需的详细信息以创建新项目。
步骤 2 − 将以下代码添加到 res/layout/activity_main.xml。
示例
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="4dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/imageView" android:layout_centerInParent="true" android:layout_marginBottom="10dp" android:text="Click the below button to take photo from camera" android:textAlignment="center" android:textColor="@android:color/holo_purple" android:textSize="16sp" android:textStyle="bold" /> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="630dp" android:layout_above="@id/btnCaptureImage" android:layout_marginTop="16dp" android:scaleType="centerCrop" android:src="@drawable/ic_baseline_image_24" /> <Button android:id="@+id/btnCaptureImage" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="Capture Image" /> </RelativeLayout>
步骤 3 − 将以下代码添加到 src/MainActivity.kt
import android.Manifest
import android.content.ContentValues
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
class MainActivity : AppCompatActivity() {
lateinit var button: Button
private lateinit var imageView: ImageView
lateinit var imageUri: Uri
private val permissionCode = 1000
private val imageCaptureCode = 1001
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
imageView = findViewById(R.id.imageView)
button = findViewById(R.id.btnCaptureImage)
button.setOnClickListener {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.CAMERA),
permissionCode
)
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.CAMERA
) != PackageManager.PERMISSION_GRANTED
) {
openCamera()
} else {
PackageManager.PERMISSION_DENIED
}
}
}
private fun openCamera() {
val values = ContentValues()
values.put(MediaStore.Images.Media.TITLE, "New Picture")
values.put(MediaStore.Images.Media.DESCRIPTION, "From the Camera")
imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)!!
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
startActivityForResult(cameraIntent, imageCaptureCode)
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String?>,
grantResults: IntArray
) {
when (requestCode) {
permissionCode -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openCamera()
} else {
Toast.makeText(this, "Permission denied...", Toast.LENGTH_SHORT).show()
}
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
imageView.setImageURI(imageUri);
}
}
}
步骤 4 − 将以下代码添加到 androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.q11"> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
让我们尝试运行您的应用程序。假设您已将实际的 Android 移动设备连接到电脑。要从 Android Studio 运行该应用程序,请打开项目的某个活动文件,然后点击工具栏上的"运行"图标
。选择您的移动设备作为选项,然后勾选将显示默认屏幕的移动设备。
注意:为了获得更好的效果,请在真实设备上试用



相关文章
打印
下一节:如何使用 Kotlin 在 Android 上的 TextView 中创建可点击链接? ❯❮ 上一节:如何使用 Kotlin 编程从我的 Android 应用程序使用 Gmail 发送电子邮件?

