Android turns up the system camera to take photos

Recently, I was looking at the picture selector imagepicker written by Nanchen. I felt that it was well written. I also planned to write down what I learned from it. Many times, a good framework can reduce development costs, which is a good thing. However, you should also understand the internal specific implementation logic. Maybe one day you need to complete a similar small function. You can write the principle quickly instead of introducing the whole framework.

This article talks about the first function: how to turn up the camera of the mobile phone to take pictures?

How to call the existing application of the system is briefly discussed here. In developing applications, calling the existing application of the system requires using Intent to specify the Action and Category of the application, then opening the specified Activity by startActivity (Intent) or startActivityForResult (Intent, int). If the startActivityForResult () method is used to open and need to return the value, then rewrite the onActivityResult (startActivityForResult, t, t).

Let's take a look at the activities defined in the androidmanifest.xml manifest file of the existing camera application of the system:

It defines two activities, com.android.camera.camera for camera and com.android.camera.videocamera for camera. It can be seen from the literal meaning that in order to capture the data returned by the system camera, you generally need to use the following two actions to start the camera and the camera:

The above two parameters are defined in the form of static constants in the mediastore class: mediastore.action_ IMAGE_ Capture (camera) and mediastore.action_ VIDEO_ Capture.

In the newly opened activity, if you need to obtain its return value, you need to use the startactivityforresult (intent, int) method to open the activity and override onactivityresult (int, intent) to obtain the return data of the system camera. Then we only need to obtain the return value in onactivityresult().

If no path is specified, the photos taken by the system camera will be saved in the system default folder, which can be obtained by using the intent. Getextra() method. The obtained URI address represents the address of a content provider. If via mediastore.extra_ Output specifies the save path, so the address obtained through intent. Getextra () will be an empty address, but since it is the address specified by us, we don't worry about not finding it.

However, if it is above 7.0, you need to use fileprovider to get the URI address.

After clarifying the process, the following is the specific code implementation:

First, declare the photographing permission under androidmanifest.xml:

After the permission is declared, we still need to judge whether the user has given us the permission to take photos before taking photos:

If the user does not give permission, you need to apply for permission. After permission application, a callback will notify the developer whether it is allowed. For details, see the distributed code:

After permission is allowed, take photos through imagepicker.takepicture. Let's take a look at the specific code logic:

The function of intent.resolveactivity is simply that when you call third-party software or system activity, such as opening a camera, sending pictures and other implicit intent, it may not work normally on all Android devices. This method is used to judge whether the activity corresponding to this intent exists to ensure that there will be no crash.

Takeimagefile is the image storage address. Before 7.0, it needs to be processed with uri.fromfile. After 7.0, fileprovider is adopted. The following describes how to use fileprovider:

To use fileprovider, you need to declare in androidmanifest.xml:

Here is the fileprovider in the V4 package used directly. We can also directly inherit the fileprovider class and override the overloaded function appropriately, but this is not recommended. Let's introduce the above settings:

For Accessible path configuration, you can create an XML file in res and a configuration file below. The format is as follows:

As explained below:

Finally, put the URI in mediastore. Extra_ In output.

After photographing, onactivityresult will be called back. Here, we can display the picture in ImageView according to the previously passed value:

At this point, the process of calling the system camera to take pictures is over.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>