Android open camera from button

前端 未结 10 1528
無奈伤痛
無奈伤痛 2020-11-29 03:24

I hope this isn\'t a duplicate question but I am making an app that I want a button to open the camera app (the default android camera separately). How do I got about doing

相关标签:
10条回答
  • 2020-11-29 03:53

    You can create a camera intent and call it as startActivityForResult(intent).

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
       // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    
    0 讨论(0)
  • 2020-11-29 03:55

    For me this worked perfectly fine .

     Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
     startActivity(intent);
    

    and add this permission to mainfest file:

    <uses-permission android:name="android.permission.CAMERA">
    

    It opens the camera,after capturing the image it saves the image to gallery with a new image set.

    0 讨论(0)
  • 2020-11-29 03:56
    public class camera_act extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_camera_act);
             ImageView imageView = findViewById(R.id.image);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent,90);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode,resultCode,data);
           Bitmap bitmap = data.getExtras.get("imageKey");
           imageView.setBitmapImage(bitmap);
                
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 04:00

    I know it is a bit late of a reply but you can use the below syntax as it worked with me just fine

    Camera=(Button)findViewById(R.id.CameraID);
    Camera.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent Intent3=new   Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
                startActivity(Intent3); 
            }
        });
    
    0 讨论(0)
  • 2020-11-29 04:01

    You are correct about the action used in Intent but it's not the only thing you have to do. You'll also have to add

    startActivityForResult(intent, YOUR_REQUEST_CODE);
    

    To get it all done and retrieve the actual picture you could check the following thread.

    Android - Capture photo

    0 讨论(0)
  • 2020-11-29 04:04
     Button b = (Button)findViewById(R.id.Button01);
    
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                 startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            }
        });
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
              Bitmap image = (Bitmap) data.getExtras().get("data");
              ImageView imageview = (ImageView) findViewById(R.id.ImageView01); //sets imageview as the bitmap
              imageview.setImageBitmap(image);
        }
    }
    
    0 讨论(0)
提交回复
热议问题