我正在创建一个简单的相机应用程序,其中单击按钮,相机打开,一旦单击图像,它将显示在图像视图中。但是,我无法显示在我的imageview中捕获的图像。以下是XML和Kotlin代码:
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome_text"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="@color/black" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/buttonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:padding="15dp"
android:text="@string/camera_text"
android:textColor="@color/black"
android:textSize="18sp"
android:layout_gravity="center_horizontal"/>
<ImageView
android:id="@+id/imageId"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop"
android:visibility="visible"
android:layout_marginTop="20dp"/>
</LinearLayout>
芬兰湾的科特林(MainActivity)
package com.example.cameraapp
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*
import java.util.jar.Manifest
class MainActivity : AppCompatActivity() {
companion object{
private const val CAMERA_PERMISSION_CODE = 1
private const val CAMERA_REQUEST_CODE = 2
}
var getImage = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val data: Intent? = result.data
if (result.resultCode == CAMERA_REQUEST_CODE) {
val thumbNail: Bitmap = data!!.extras!!.get("data") as Bitmap
imageId.setImageBitmap(thumbNail)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
buttonId.setOnClickListener {
if(ContextCompat.checkSelfPermission(
this, android.Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED ){
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
getImage.launch(intent)
}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)
getImage.launch(intent)
}else{
Toast.makeText(this@MainActivity, "Permission Denied!!",
Toast.LENGTH_LONG).show()
}
}
}
}
请帮我找到一个解决方案。谢谢!
JAVA CODE
private void takeImageFromCamera() {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, getString(R.string.new_picture));
values.put(MediaStore.Images.Media.DESCRIPTION, getString(R.string.from_your_camera));
cameraUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri);
intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
startActivityForResult(intent, CAMERA_IMAGE_CODE);
photoLinearLayoutVw.setVisibility(View.GONE);
dimImageVw.setVisibility(View.GONE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
try {
this.requestCode = requestCode;
this.resultCode = resultCode;
this.data = data;
descEditText.setText("");
descriptionEditDialogVw.setVisibility(View.VISIBLE);
} catch (Exception e) {
L.wtf(e);
}
} else {
Toast.makeText(getApplicationContext(), getString(R.string.unable_fetch_image), Toast.LENGTH_LONG).show();
}
}
您可能想要启动一个ActivityResultContracts.TakePicture()
,ActivityResultContract
.
首先,定义一个Uri
来保存图像:
private val imageFile = File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "filename")
private val uri = FileProvider.getUriForFile(
this,
"${applicationContext.packageName}.provider",
imageFile
)
然后,注册你的活动监听器:
private val getImage = registerForActivityResult(ActivityResultContracts.TakePicture()) { success ->
if (success) {
// Image is available at the specified uri...
}
}
最后,使用:
启动ActivityResultContract
getImage.launch(uri)
确保您已经在AndroidManifest.xml
文件中定义了必要的FileProvider
。
有关ActivityResultContracts.TakePicture
的更多信息,请参阅官方文档