Use Camera in Android Studio

Reading Time: 2 minutes

There are many in-built functionalities in Android mobile devices. In this tutorial it is explained how to use camera in android studio. Once one activity using camera is added to main project, go to main java project for this activity (ActivityCamera.java) and change there

public class ActivityCamera extends AppCompatActivity {
to
public class ActivityCamera extends Activity {
Also put under the comment
//setSupportActionBar(toolbar);
Inside main class declare new image button.

public class ActivityCamera extends Activity {

ImageButton imgButton;

Then, connect this imgButton with real control.
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_activity_camera);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

//setSupportActionBar(toolbar);

imgButton = (ImageButton) findViewById(R.id.imageButton5);

This imgButton now needs following setOnclickListener:

imgButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

}

});

But if we want not just start the camera, but control place where picture will be saved and with which name, we can add method for working with files:

private File getFile()
{

File folder = new File(“sdcard/camera_app”) ;

if(!folder.exists())

{

folder.mkdir();

}

File image_file = new File(folder,”cam_image.jpg”);

return image_file;

}

Now, we can continue writing code for setOnclickListener:

Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));

So, with this code, setOnclickListener looks like this:

imgButton.setOnClickListener(new View.OnClickListener() {

//@Override

public void onClick(View v) {

Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

File file = getFile();

camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));

}

});

Now declare:

public class ActivityCamera extends Activity {

ImageButton imgButton;

static final int CAM_REQUEST = 1;

And finalize the code:

imgButton.setOnClickListener(new View.OnClickListener() {
//@Override
public void onClick(View v) {

Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

File file = getFile();

camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));

startActivityForResult(camera_intent,CAM_REQUEST);

}

Now our code is functional. We can achieve just starting camera without controlling path and name of the picture made by this short code:

int IMAGE_CAPTURE = 102;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, IMAGE_CAPTURE);

We can use intent class to preview images. For that purpose, we can write new method for new image button. Let’s call this method public void startPreview(View view)

Code is following:
public void startPreview(View view)
{

Intent intent = new Intent();

intent.setAction(android.content.Intent.ACTION_VIEW);

intent.setType(“image/*”);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

}

When intent object is initialized, we must set action to be .ACTION_VIEW since  we don’t want this time to create or edit images.

External links:

Use Intent class to preview images on Stackoverflow
use camera in android studio on blog.rhesoft.com
use camera in android studio on Youtube

Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*