如何在 Android 中以编程方式点击相机?
本例演示了如何
步骤 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 运行应用程序,请打开项目中的一个活动文件,然后单击工具栏中的运行图标 。选择您的移动设备作为选项,然后查看将显示默认屏幕的移动设备。
注意:在真正的设备上尝试操作以获得更好的结果
广告