SoFunction
Updated on 2025-04-07

Steps to take photos with Android using the system camera

Preface

In our daily development, we sometimes encounter the need to use the camera, and the camera is also a very common thing, such as scanning the QR code, taking photos and uploading them, etc. Here I will not talk about the powerful photography function that is as customized as QQ (in fact, I don’t know either). I will talk about the simplest way to call the system camera to take photos and store them.

Steps to call the system camera

Here I will use a simple example to explain this content.
I wrote a demo myself, and the layout is very simple:

<Button
 android:
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="4dp"
 android:text="take phone"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintHorizontal_bias="0.281"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toTopOf="parent" />

 <ImageView
 android:
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="29dp"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toBottomOf="@+id/button"
 app:srcCompat="@mipmap/ic_launcher_round" />

It is just a button click to pop up the camera, and then an imageView displays the photos taken.

Next I think about what we need to do in the entire process of calling:
First of all, if you pop up the camera, you must jump to the camera application, so you must start the camera's activities by stealthily.
Then when we return to the application, we also need to display the photos, so we need to use the startActivityForResult method here.
Secondly, we must store the photos after we take pictures, so it involves the operation of the files.
When it comes to memory operations, you must deal with permissions, and all the content related to permissions is still available.
Finally, there is another question: after taking photos, we need to store photos, so we have to give it an address uri, but can we send it directly as a parameter? Here we need to use a special content provider FileProvider.
The above is what you need to use to call the camera. Although it is very shallow, it will all be involved. Next, let’s see how to implement it. Take a look at the code of onCreate in Activity:

 private Uri imageUri;
 private ImageView imageView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 setContentView(.activity_main);
 imageView = findViewById();

 Button button = findViewById();
 (new () {
  @Override
  public void onClick(View v) {

  //Create a File object.  getExternalCacheDir() gets the cache directory of the application, which is the photo name  File outputImage = new File(getExternalCacheDir(),"");
  try{  
  //Create an empty file   ();
  }catch (IOException e){
   ();
  }

  // Different Android versions use different ways to obtain Uri  if (.SDK_INT&gt;=24){
   imageUri =(,"huan",outputImage);
  }else{
   imageUri = (outputImage);
  }

  //Start the corresponding activity of the camera  Intent intent = new Intent(".IMAGE_CAPTURE");
  (MediaStore.EXTRA_OUTPUT,imageUri);
  startActivityForResult(intent,1);
  }
 });

Let's take a look at the code here: the previous code is very simple, the initialization of the control.
We know that photos need to be placed in folders, so we need to create a File object here, specifying the path and name of the file. Why use getExternalCacheDir() for this path? Because each application has a corresponding cache directory, you do not need to access memory permissions when accessing these directories, so that you can save the steps required for permissions. This directory is in /scare/Android/data//cache.

Then we create an empty folder. If there are already photos here, for example, when we take a photo for the second time, we will not create a new empty folder. It will not be replaced until it is stored.

Then we just mentioned that if the pictures taken are displayed in our application, the content provider must be used. Here we use FileProvider to get uri. I have mentioned the following article.
If it is Android version 4.4, use the (outputImage); method to get uri

Then you can turn on the camera by implicitly starting the camera activity. The action of the system camera here is .IMAGE_CAPTURE, the parameter name of the camera storage path is MediaStore.EXTRA_OUTPUT, and the uri is transmitted in.

Well, this is the process of taking photos and storing photos. What's missing next? By the way, show the photos. Now there is this photo in memory, and Uri knows it, so it's easy, look at the code:

 @Override
 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
 if (requestCode == 1) {
  if (resultCode == RESULT_OK)
  try {
   Bitmap bitmap = (getContentResolver().openInputStream(imageUri));
   (bitmap);
  } catch (FileNotFoundException e) {
   ();
  }
 }
 }

We just used startActivityForResult to start the activity, so we need to rewrite this method to display the picture. Here we first determine which startup command is, and then determine whether it is successfully started. Then use this method to obtain the bitmap and then display the bitmap. This method requires a stream, and you can open a stream by using the getContentResolver().openInputStream method.

At this point, the entire process is solved.

FileProvider

FileProvider is a special content provider that can change a uri starting with a file to a content beginning, for example: file://uri -> content://uri. Then why do you do this? Let me briefly talk about it here:
This is because after Android 7.0, the official prohibits directly transmitting a real-path uri to other applications, and we have to send the address to the camera, so there will be problems. For details, please refer to this blog:Android 7.0 behavior changes Share files between applications through FileProvider

Since it is a content provider, you must register:

<provider
  android:name="."
  android:authorities="huan"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name=".FILE_PROVIDER_PATHS"
  android:resource="@xml/file_paths" />
 </provider>

The authorities parameter here must be consistent with the second parameter of the previous getUriForFile method, and the authorities of the same content provider must be the same. The grantUriPermissions parameter must be true. The roughly means that all elements it gives authorization to be accessed. In FileProvider, this parameter must be true (this is also one of the reasons why Android versions below 4.4 cannot be used. If you are interested, you can learn about it. The export parameter indicates whether it can be shared with other applications. It should be set to false here. <meta-data is to configure the file path that we can access, and @xml/file_paths is to indicate which file can be accessed. Of course, we need to create this file. Look at the code:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:andro>
 <external-path
 name="my_image"
 path="/"/>
</paths>

<external-path means the path that can be accessed. Name is used for the subsequent mapping, so you can start it at will. I use a horizontal bar here to indicate that the entire directory can be accessed.

summary

Although it is not difficult to call the system camera function and not much code, there is a lot of fragmented knowledge and scattered knowledge, so you should pay attention to it. Especially when it comes to the low-end Android version, you should pay special attention to it.

The above is the detailed content of the steps for Android to take photos using the system camera. For more information about Android's applicable system camera, please pay attention to my other related articles!