Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9173367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:34:24+00:00 2026-06-17T16:34:24+00:00

I have created an app with a button and wrote onClickListener for that button.

  • 0

I have created an app with a button and wrote onClickListener for that button. I have tried several sample code examples and none of them worked. They all bring up the Android camera app and don’t take photographs. I want some code which I can put in my onClickListener so when I press the button on the screen, a picture will be taken.

How can I make the camera take a picture when I press a button in an Android activity?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:34:25+00:00Added an answer on June 17, 2026 at 4:34 pm

    Look at following demo code.

    Here is your XML file for UI,

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/btnCapture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Camera" />
    
    </LinearLayout>
    

    And here is your Java class file,

    public class CameraDemoActivity extends Activity {
        int TAKE_PHOTO_CODE = 0;
        public static int count = 0;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            // Here, we are making a folder named picFolder to store
            // pics taken by the camera using this application.
            final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
            File newdir = new File(dir);
            newdir.mkdirs();
    
            Button capture = (Button) findViewById(R.id.btnCapture);
            capture.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
    
                    // Here, the counter will be incremented each time, and the
                    // picture taken by camera will be stored as 1.jpg,2.jpg
                    // and likewise.
                    count++;
                    String file = dir+count+".jpg";
                    File newfile = new File(file);
                    try {
                        newfile.createNewFile();
                    }
                    catch (IOException e)
                    {
                    }
    
                    Uri outputFileUri = Uri.fromFile(newfile);
    
                    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    
                    startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
                }
            });
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
                Log.d("CameraDemo", "Pic saved");
            }
        }
    }
    

    Note:

    Specify the following permissions in your manifest file,

    <uses-permission android:name="android.permission.CAMERA"/>
    
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several buttons on my app that are being created dynamically. They are
I've created an app that shows 8 buttons in coverflow using icarousel.i have to
I have created an app in C# .Net 2.0 that uses the AxShockwaveFlash object
I have created an app that uses NSTimer, which gets triggered each second. My
I have an app that I wrote using C# .NET 4.0 in Visual Studio
In my App I have a ParentViewController that have several elements in it. One
So I have created an app(simple IDE) where you can write/edit ada code and
i have just created a drum app. The user taps on the individual buttons
I have created the app with forge, and have added in the chrome extension,
I have created an App Widget for Android 1.5. It uses a TextView to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.