How to take a picture to show in a `ImageView` and save the picture?

后端 未结 4 709
执笔经年
执笔经年 2020-12-28 23:16

I need to take a picture with the camera, save the picture, show in ImageView and when I click the Imagevie

4条回答
  •  攒了一身酷
    2020-12-28 23:51

    MainActivity.class

    package edu.gvsu.cis.masl.camerademo;
        import android.app.Activity;
        import android.content.Intent;
        import android.graphics.Bitmap;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.ImageView;
        public class MyCameraActivity extends Activity {
            private static final int CAMERA_REQUEST = 1888; 
            private ImageView imageView;
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                this.imageView = (ImageView)this.findViewById(R.id.imageView1);
                Button photoButton = (Button) this.findViewById(R.id.button1);
                photoButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                        startActivityForResult(cameraIntent, CAMERA_REQUEST); 
                    }
                });
            }
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
                if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
                    Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                    imageView.setImageBitmap(photo);
                }  
            } 
        }
    

    main.xml

    
    
    
        
    
        
        
    
    
    

    Make sure your all id would be correct.

    Anything you need to know, hassle free to contact me.

提交回复
热议问题