Android调用相机拍照,展示拍摄的图片

调用相机(隐式调用)

//自定义一个请求码 这里我设为10010
 int TAKE_PHOTO_REQUEST = 10010;
 int RESULT_CANCELED = 0;//定义取消码
 //触发监听,调用相机
 image_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            //创建一个意图,这里指的是相机
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //开启这个意图,并将状态码传给目标Activity
                startActivityForResult(intent, TAKE_PHOTO_REQUEST);
            }
        });

展示相机拍摄的图片

重写onActivityResult方法

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //在日志中打印当前的请求码和返回码
        Log.i("TAG", "resultCode:"+resultCode);
        Log.i("TAG", "requestCode:"+requestCode);

        if (resultCode == RESULT_CANCELED) {
            if (requestCode==TAKE_PHOTO_REQUEST)
                Toast.makeText(MainActivity.this, "取消了拍照", Toast.LENGTH_LONG).show();
        }
        if (requestCode == TAKE_PHOTO_REQUEST){
            if (data!=null){
            //将返回的图片数据使用Bitmap类型变量targetBitmap 接收
                Bitmap targetBitmap = data.getParcelableExtra("data");
                //将targetBitmap 展示在ImageView组件imageView_test上
                imageView_test.setImageBitmap(targetBitmap);
            }
        }
    }

完整代码


import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    //自定义一个请求码 这里我设为10010
    int TAKE_PHOTO_REQUEST = 10010;
    int RESULT_CANCELED = 0;//定义取消码
    ImageView imageView_test;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView_test = findViewById(R.id.imageView_test);
    findViewById(R.id.image_camera).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //创建一个意图,这里指的是相机
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //开启这个意图,并将状态码传给目标Activity
            startActivityForResult(intent, TAKE_PHOTO_REQUEST);
        }
    });

    }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //在日志中打印当前的请求码和返回码
    Log.i("TAG", "resultCode:"+resultCode);
    Log.i("TAG", "requestCode:"+requestCode);

    if (resultCode == RESULT_CANCELED) {
        if (requestCode==TAKE_PHOTO_REQUEST)
            Toast.makeText(MainActivity.this, "取消了拍照", Toast.LENGTH_LONG).show();
    }
    if (requestCode == TAKE_PHOTO_REQUEST){
        if (data!=null){
            //将返回的图片数据使用Bitmap类型变量targetBitmap 接收
            Bitmap targetBitmap = data.getParcelableExtra("data");
            //将targetBitmap 展示在ImageView组件imageView_test上
            imageView_test.setImageBitmap(targetBitmap);
        }
    }
}
}

效果演示

Android调用相机拍照,展示拍摄的图片_第1张图片

你可能感兴趣的:(数码相机,android)