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
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);
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.
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);
}
}
}
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);
}
});
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
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);
}
}