Android implements the method of calling camera and photo album

It's convenient for Android to call the camera. Take a look at the interface first

Layout file activity_ Main.xml source code

Because it involves writing data to the SD card, you need to declare the response permission in androidmainfest.xml

Mainactivity.java source code

Call the camera to take pictures

Naturally, the first thing to do in mainactivity is to obtain the instances of button and ImageView respectively, register the click event for the button, and then start processing the logic of calling the camera in the click event of the button. Let's focus on this part of the code. First, a file object is created to store the pictures taken by the camera. Here, we name the picture saveimage.jpg and store it in the root directory of the mobile phone SD card. The root directory of the mobile phone SD card is obtained by calling getexternalstoragedirectory() method of environment. Then call the fromfile () method of URI to convert the file object into a URI object, which identifies the unique address of saveimage.jpg. Then build an intent object and specify the action of the intent as android.media.action.image_ CAPTURE, then call the putExtra () method of Intent to specify the output address of the picture, fill in the Uri object just now, and finally call startActivityForResult () to start the activity. Because we use an implicit intent, the system will find out the activities that can respond to this intent to start, so that the camera program will be opened and the photos taken will be output to saveimage.jpg.

Note that we used startactivityforresult() to start the activity just now, so the result will be returned to onactivityresult() method after taking the picture. If it is found that the photographing is successful, an intent object will be constructed again and its action will be specified as com.android.camera.action.crop. This intent is used to crop the photos. Note that we just used startactivityforresult() to start the activity, so the results will be returned to the onactivityresult() method after taking the photos. If it is found that the photographing is successful, an intent object will be constructed again and its action will be specified as com.android.camera.action.crop. This intent is used to cut out photos

Select photos from album

In the click event of the "select pictures from album" button, we also created a file object to store the pictures selected from the album. Then build an intent object and specify its action as android.intent.action.get_ CONTENT。 Then set some necessary parameters for the intent object, including whether to allow scaling and clipping, the output position of the picture, etc. Finally, by calling the startActivityForResult () method, you can open the photo album program and select the photo.

Note that when calling the startactivityforresult () method, the value we passed in for the second parameter is still crop_ The photo constant has the advantage that after selecting photos from the album, you will directly enter crop_ The picture will be displayed under the photo case, so that the logic of displaying the picture written before can be reused without writing it again.

reference material

1. Chapter of calling camera in the first line of code - Android

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>