Android手机拍照功能实现

1.Photo.xml文件设置布局空间:ImageView 和 Button  实现点击按钮显示照片




        

        

2.java文件配置代码

package com.example.bf.photo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    ImageView photoiv;
    Button photobtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        photoiv=findViewById(R.id.photoiv);
        photobtn=findViewById(R.id.photobtn);

        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            if (requestCode == 1) {
                    if (resultCode == Activity.RESULT_OK) {
                        Bitmap bitmap = (Bitmap)data.getExtras().get("data");
                        photoiv.setImageBitmap(bitmap);
                    }
            }
        }

        photobtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,1);
            }
        });

    }
}

你可能感兴趣的:(安卓)