Capture Image from Camera and Display in Activity

前端 未结 16 1038
悲&欢浪女
悲&欢浪女 2020-11-21 06:43

I want to write a module where on a click of a button the camera opens and I can click and capture an image. If I don\'t like the image I can delete it and click one more i

16条回答
  •  无人及你
    2020-11-21 07:09

    Here is the complete code:

    package com.example.cameraa;
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity {
    
    
    
    
            Button btnTackPic;
            Uri photoPath;
            ImageView ivThumbnailPhoto;
    
            static int TAKE_PICTURE = 1;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
                // Get reference to views
    
                btnTackPic = (Button) findViewById(R.id.bt1);
                ivThumbnailPhoto = (ImageView) findViewById(R.id.imageView1);
    
    
    
    
         btnTackPic.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
    
                    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                    startActivityForResult(cameraIntent, TAKE_PICTURE); 
                }
    
    
    
    
        });
    
            } 
    
            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    
    
                    if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {  
                        Bitmap photo = (Bitmap)intent.getExtras().get("data"); 
                       ivThumbnailPhoto.setImageBitmap(photo);
                    ivThumbnailPhoto.setVisibility(View.VISIBLE);
    
    
    
                }
            }
    }
    

    Remember to add permissions for the camera too.

提交回复
热议问题