Taking pictures with Android software

I created the application using a button and wrote onClickListener for this button. I tried some sample code examples and none of them worked. All of them bring up the application for the Android camera and do not take pictures. I want some code that I can put in my onClickListener, so when I click on the button on the screen, a snapshot will be taken.

How to take a camera snapshot when I press a button in Android activity?

+48
android image camera
Jan 20 '13 at 4:32
source share
7 answers

Check out the following demo code.

Here is your XML file for the user interface,

<?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"/> 
+89
Jan 20 '13 at 4:59
source share

There are two ways to take a picture:

1 - Using the intent to take a picture

2 - Using the camera API

I think you should use the second method, and here is a sample code for two of them.

+25
Jan 20 '13 at 4:40
source share

You can use the Magical Take Photo library.

1. try compiling in gradle

 compile 'com.frosquivel:magicaltakephoto:1.0' 

2. You need this permission in your manifest. Xml

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

3. an instance of a class like this

// "this" is the current activity parameter

 MagicalTakePhoto magicalTakePhoto = new MagicalTakePhoto(this,ANY_INTEGER_0_TO_4000_FOR_QUALITY); 

4. if you need to take a picture, use the method

 magicalTakePhoto.takePhoto("my_photo_name"); 

5. if you need to select an image on the device, try using the method:

 magicalTakePhoto.selectedPicture("my_header_name"); 

6. You need to override the onActivityResult method of the activity or fragment as follows:

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); magicalTakePhoto.resultPhoto(requestCode, resultCode, data); // example to get photo // imageView.setImageBitmap(magicalTakePhoto.getMyPhoto()); } 

Note. Only in this library can you select and select an image on the device using the min API 15.

+4
May 16 '16 at 10:54 pm
source share

For those who come here, looking for a way to take snapshots / photos programmatically using both the Android Camera and Camera2 APIs, see the open source example provided by Google itself here .

+2
Apr 10 '17 at 2:22 on
source share

Take and save the image in the desired folder

  //Global Variables private static final int CAMERA_IMAGE_REQUEST = 101; private String imageName; 

Take image function

  public void captureImage() { // Creating folders for Image String imageFolderPath = Environment.getExternalStorageDirectory().toString() + "/AutoFare"; File imagesFolder = new File(imageFolderPath); imagesFolder.mkdirs(); // Generating file name imageName = new Date().toString() + ".png"; // Creating image here Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(imageFolderPath, imageName))); startActivityForResult(takePictureIntent, CAMERA_IMAGE_REQUEST); } 

Broadcast new image added otherwise pic will not be displayed in the image gallery

  public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK && requestCode == CAMERA_IMAGE_REQUEST) { Toast.makeText(getActivity(), "Success", Toast.LENGTH_SHORT).show(); //Scan new image added MediaScannerConnection.scanFile(getActivity(), new String[]{new File(Environment.getExternalStorageDirectory() + "/AutoFare/" + imageName).getPath()}, new String[]{"image/png"}, null); // Work in few phones if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(Environment.getExternalStorageDirectory() + "/AutoFare/" + imageName))); } else { getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(Environment.getExternalStorageDirectory() + "/AutoFare/" + imageName))); } } else { Toast.makeText(getActivity(), "Take Picture Failed or canceled", Toast.LENGTH_SHORT).show(); } } 

Access rights

  <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+1
Jan 11 '17 at 4:48 on
source share

Below is an easy way to open the camera:

 private void startCamera() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath()); startActivityForResult(intent, 1); } 
-one
Aug 05 '14 at 5:59
source share
 Intent takePhoto = new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(takePhoto, CAMERA_PIC_REQUEST) 

and set CAMERA_PIC_REQUEST= 1 or 0

-3
Mar 20 '16 at 6:41
source share



All Articles