How to use phone camera in an android app

August 22, 2012 | Rishabh Agrawal | General

If you want your android app to use phone camera and capture an image then you need to create a camera intent in the following way.
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

CAMERA_REQUEST is integer which contains your request number.

The following example code explains its usage.

Button cam_button,gal_button;
static Bitmap bMap;
private static final int CAMERA_REQUEST = 1888;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_screen);
cam_button=(Button) findViewById(R.id.camera);
cam_button.setOnClickListener(this);

}

@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, Intent imageReturnedIntent)
{

if (requestCode == CAMERA_REQUEST)
{
bMap = (Bitmap) data.getExtras().get(“data”);

}
}

When onActivityResult is called ,if requestCode matches our CAMERA_REQUEST then image is stored in bMap of type Bitmap. bMap can be later used in the app.

Hope this post is helpful.

Posted by: Rachita Nanda


THE AUTHORRishabh Agrawal

Rishabh Agrawal is the founder of Creatiosoft, a company focused on creating high-quality software for the iGaming industry, specialising in poker and card games. With years of experience, Rishabh is dedicated to delivering engaging and user-friendly gaming experiences. Through this blog, he shares his passion and insights to help readers understand the latest trends and advancements in iGaming.

Comments are closed.

Recent Posts

The Future of Mobile Gaming: What to Expect in the Next 5 Years

Over the years, mobile gaming has seen amazing development and innovation utilized by the game development company in India. It…
06 Jun 2023 Rishabh Agrawal

How Do I Get Started With NFT Gaming?

The popularity of the NFT game has created a different fan base. There was a time when games were a…
18 Jul 2022 Rishabh Agrawal

What Are The Top Trends in NFT Marketplace 2022?

The NFTs are the new engaging and revolutionary technology across the globe. Though, these non-fungible tokens appeared for the first…
09 Jun 2022 Rishabh Agrawal