Android to take pictures using the system camera

  • 2021-12-11 08:52:48
  • OfStack

Preface

In our daily development, we sometimes encounter the need to use cameras, and cameras are also very common things, such as scanning 2D codes, taking photos and uploading them, and so on. Here I don't talk about the powerful custom photo function like qq (in fact, I won't either), but the simplest call system camera to take photos and save them

Invoke system camera step

Here I will talk about this content through a simple example.
I wrote an demo myself, and the layout is very simple:


<Button
 android:id="@+id/button"
 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:id="@+id/imageView"
 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" />

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

Next, I think what we need to do in the whole process of calling 1:
First of all, if you pop up the camera, you must jump to the camera application, so you must start the camera activity implicitly.
Then when we return to the application, we will also display the photos, so we will use the startActivityForResult method here.
Secondly, we must store it after taking pictures, so it involves the operation of files.
When it comes to memory operations, you must deal with permissions, all of which are related to permissions.
Finally, there is a question that the camera should store photos after taking photos, so we should give him an address uri, but can we send the address directly as a parameter? The special content provider FileProvider is used here.
The above is the content to be used to call the camera. Although it is very shallow, it will be involved. Next, let's see how to achieve it. Look at the code for onCreate in Activity:


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

 Button button = findViewById(R.id.button);
 button.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {

  // Create 1 A File Object. getExternalCacheDir() Gets the cache directory of the application, outputImage.jpg Is the name of the photo 
  File outputImage = new File(getExternalCacheDir(),"outputImage.jpg");
  try{  
  // Create 1 Empty files   
   outputImage.createNewFile();
  }catch (IOException e){
   e.printStackTrace();
  }

  // Different Android versions use different access Uri Method of 
  if (Build.VERSION.SDK_INT>=24){
   imageUri =FileProvider.getUriForFile(MainActivity.this,"huan",outputImage);
  }else{
   imageUri = Uri.fromFile(outputImage);
  }

  // Start the correspondence of the camera Activity
  Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
  intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
  startActivityForResult(intent,1);
  }
 });

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

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

Then we just mentioned that if the pictures are to be displayed in our application, we must use the content provider. FileProvider is used here to get uri, and I will talk about provider below.
If it is Android lower than version 4.4, use Uri. fromFile (outputImage); Method can get uri

You can turn on the camera by implicitly starting the camera activity. Here, the action of the system camera is android.media.action.IMAGE_CAPTURE, and the parameter name of the camera storage path is MediaStore.EXTRA_OUTPUT, and uri is transmitted in.

Ok, this completes the steps of taking pictures and storing them. What's next? By the way, show the photos. Now you have this picture 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 = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
   imageView.setImageBitmap(bitmap);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }
 }

We just used startActivityForResult to start the activity, so we will override this method to display the picture. Here, first judge which startup command is, then judge whether it starts successfully, then get bitmap by BitmapFactory. decodeStream, and then display bitmap. The BitmapFactory. decodeStream method requires a stream, which can be opened by the getContentResolver (). openInputStream method.

At this point, the whole process is solved.

FileProvider

FileProvider is a special content provider. You can change an uri beginning with file to an content beginning, for example: file://uri- > content://uri. Then why do you want to do this? Here is a brief talk:
This is because after Android 7.0, it is officially forbidden to directly transmit uri with a real path to other applications, and we have to send the address to the camera, so there will be problems. For more details, please refer to this blog: Android 7.0 Behavior Changes Share Files Between Applications through FileProvider

Since it is a content provider, it must be registered:


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

Here, the authorities parameter must be the same as the second parameter of the previous getUriForFile method, and the authorities of the same content provider must be the same. grantUriPermissions parameter 1 must be true, which roughly means that all its elements can be authorized to be accessed. In FileProvider, this parameter must be true (which is one of the reasons why Android version 4.41 cannot be used. If you are interested, you can learn about 1). export indicates whether it can be shared by other applications, and it should be set to false here. < meta-data This is the file path that we can access. @ xml/file_paths This is what file can be accessed. Of course, we should build one of these files. Look at the code:


<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
 <external-path
 name="my_image"
 path="/"/>
</paths>

< external-path This is the path that can be accessed, and name is used for mapping behind, so you can start at will. I use 1 crossbar here to indicate that the whole directory can be accessed.

Summary

Although it is not difficult to call the function of the system camera, and there are not many codes, there are many piecemeal knowledge, which should be paid attention to. In particular, we should pay special attention to the Android version with low and high matching.

The above is the Android using the system camera to take photos of the details of the steps, more about Android applicable system camera information please pay attention to other related articles on this site!


Related articles: