Original link: http://www.orlion.ga/665/
First, call the camera
Create a project Choosepicdemo, modify Activity_main.xml:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent "android:layout_height=" match_parent "android:orientation=" vertical " > <button android:id= "@+id/take_photo" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= "Take Photo " /> <ImageView android:id= "@+id/picture" android:layout_ Width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_gravity= "Center_horizontal" /></ Linearlayout>
Where button is used to pick up the camera, ImageView is used to display the captured picture
Mainactivity:
Package ga.orlion.choosepicdemo;import java.io.file;import java.io.filenotfoundexception;import java.io.IOException;import android.app.Activity;import android.content.Intent;import android.graphics.bitmap;import android.graphics.bitmapfactory;import android.net.uri;import android.os.bundle;import android.os.environment;import android.provider.mediastore;import Android.view.view;import android.widget.button;import android.widget.imageview;public class mainactivity extends activity {public static final int take_photo = 1;public static final int CROP_PHOTO = 2;private Button takephoto;private imageview picture;private uri imageuri; @Overrideprotected void oncreate (bundle savedinstancestate) {super.oncreate (savedinstancestate); SetContentView ( R.layout.activity_main); takephoto =&nbSP; (Button) findviewbyid (R.id.take_photo);p icture = (ImageView) findviewbyid (r.id.picture) ; Takephoto.setonclicklistener (New view.onclicklistener () {@Overridepublic void onclick ( VIEW&NBSP;V) {// Create a File object to store the photo file outputimage = new file after taking photos ( Environment.getexternalstoragedirectory () , "Tempimage.jpg");try {if (Outputimage.exists () ) {outputimage.delete ();} Outputimage.createnewfile ();} catch (ioexception e) {e.printstacktrace ();} Imageuri = uri.fromfile (Outputimage); Intent intent = new intent (" Android.media.action.IMAGE_CAPTURE "); Intent.putextra (Mediastore.extra_output, imageuri); Startactivityforresult (Intent, take_photo); // Start Camera Program}}); @Overrideprotected void onactivityresult (int requestcode, int resultcode, intent data) {switch (Requestcode) {case TAKE_PHOTO:if (resuLTCODE&NBSP;==&NBSP;RESULT_OK) {intent intent = new intent (" Com.android.camera.action.CROP "), Intent.setdataandtype (imageuri, " image/* "), Intent.putextra (" scale ", true); Intent.putextra (Mediastore.extra_output, imageuri); Startactivityforresult (intent , Crop_photo);} break;case crop_photo:if (RESULTCODE&NBSP;==&NBSP;RESULT_OK) {try {bitmap bitmap = bitmapfactory.decodestream (Getcontentresolver (). Openinputstream (Imageuri));p Icture.setimagebitmap (bitmap);} catch (filenotfoundexception e) {e.printstacktrace ();}} Break;default:break;}}}
The first thing to do in mainactivity is to get the instance of button and ImageView separately, to register the button with the Click event, and then start processing the logic of invoking the camera in the Click event of the button, we focus on this part of the code.
First, a file object is created to store the image taken by the camera, where we name the image tempimage.jpg and store it in the root directory of the phone's SD card, calling Environment's Getexternalstora The Gedirectory () method gets the root directory of the phone's SD card. The FromFile () method of the URI is then called to convert the file object to a URI object that identifies the unique address of the tempimage.jpg image. The intent object is then constructed, and the intent action is specified as Android.media.action.IMAGE_CAPTURE, and the Intent PutExtra () method is called to specify the output address of the picture. This fills in the URI object that you just got, and finally calls Startactivityforresult () to start the activity. Since we are using an implicit intent, the system will find the activity that can respond to this intent to start, so the camera program will be opened, the photos taken will be output to tempimage.jpg.
Note that we started the activity by using Startactivityforresult (), so the results are returned to the Onactivityresult () method after the photo is taken. If a photo is found to be successful, a intent object is built again, and its action is specified as Com.android.camera.action.CROP. This intent is used to crop the photos taken, because the cameras take a larger picture, and we may only want to intercept a small fraction of them. and give this
Intent set some necessary properties and call Startactivityforresult () again to start the clipping program. The cropped photo is also output to the tempimage.jpg. After the cropping operation is complete, the program will then call back to the Onactivityresult () method, and this time we can invoke the Bitmapfactory Decodestream () method to parse the tempimage.jpg image into a bitmap object. Then set it to ImageView to show it. Since this project involves the operation of writing data to the SD card, we also need to declare the permissions in Androidmanifest.xml:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
Second, select photos from the album
First, add a button to the Activity_main.xml:
<button android:id= "@+id/choose_from_album" android:layout_width= "Match_parent" android:layout_he ight= "Wrap_content" android:text= "Choose from album"/>
Then bind the event in Mainactivity:
choosefromalbum = (Button) findviewbyid (r.id.choose_from_album); Choosefromalbum.setonclicklistener (New onclicklistener () {@Overridepublic void onclick ( VIEW&NBSP;V) { // Create a File object to store selected photos File Outputimage = new file (Environment. getexternalstoragedirectory (), "Output_image.jpg"); try { if (Outputimage.exists ()) { Outputimage.delete (); } outputimage.createnewfile (); } catch (IOException e) { e.printstacktrace (); } imageuri = uri.fromfile (outputimage); intent intent = new intent ("Android.intent.action.GET_CONTENT"); intent.settype ("image/*"); intent.putextra ("crop", true); intent.putextra ("scale", true); intent.putextra (MediaStore.EXTRA_ Output, imageuri); startactivityforresult (Intent, crop_photo); }});
As you can see, in the Click event of the Choose fromalbum button We also created a file object to store the pictures selected from the album. It then constructs a Intent object and assigns its action as Android.intent.action.GET_CONTENT. Next, set some necessary parameters for the intent object, including whether to allow zooming and cropping, the output position of the picture, and so on. Finally call the Startactivityforresult () method, you can open the album program to select Photos.
Android Getting Started (16) Call Camera album