Many applications may use the ability to call the camera to take photos and pick pictures from the album, today we start the Android camera with album Call Learning.
Directory Navigation
- Call camera to take pictures
- Select a photo from the album
- Friendship Link
Call camera to take pictures
Approximate process: Call the camera to take a picture, then clip it, and then display it in the ImageView. The project structure is as follows:
First, some of the initialization code:
Public Static Final intTake_photo = 1; Public Static Final intCrop_photo = 2; Public Static Final intChoose_photo = 3;PrivateImageView picture;PrivateUri Imageuri; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); picture=(ImageView) Findviewbyid (r.id.picture);}
Second, create the file:
// Create a File object to store pictures after taking pictures Private file Createimagefile (String fileName) { new file (environment.getexternalstoragedirectory (), fileName); Try { if (outputimage.exists ()) { outputimage.delete (); } Outputimage.createnewfile (); Catch (IOException e) { e.printstacktrace (); } return outputimage;}
Third, call the system camera to take pictures,
// call the system camera to take a picture and create a JPG file Public void takephone (view view) { = Uri.fromfile (Createimagefile ("takephone.jpg")); New Intent ("Android.media.action.IMAGE_CAPTURE"); Intent.putextra (Mediastore.extra_output, Imageuri); // Start the camera program }
Iv. Methods of Onactivityresult:
@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { Switch(requestcode) { CaseTake_photo:phonetake (ResultCode); Break; CaseCrop_photo:phonecrop (ResultCode); Break; CaseChoose_photo:phonechoose (ResultCode, data); Break; default: Break; }}
Five, Phonetake method:
// camera callback void phonetake (int ResultCode) { if (resultcode == 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); // start clipping program
Vi. Phonecrop method, clipping callback
//clipped callbacksPrivate voidPhonecrop (intResultCode) { if(ResultCode = =RESULT_OK) { Try{bufferedinputstream InputStream=NewBufferedinputstream (Getcontentresolver (). Openinputstream (Imageuri)); Bitmap Bitmap=Bitmapfactory.decodestream (InputStream); Picture.setimagebitmap (bitmap); //show the cropped photo}Catch(FileNotFoundException e) {e.printstacktrace (); } }}
Vii. Declaration of authority:
<android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>
Select a photo from the album
One, select photos from the album:
// Select a photo from the album: Public void choosephone (view view) { = Uri.fromfile (Createimagefile ("choosephone.jpg")); New Intent (intent.action_get_content); Intent.settype ("image/*"); Intent.putextra (true); Intent.putextra (true); Startactivityforresult (Intent, Choose_photo);}
Second, select a picture from the album, and then show
//Select a picture from the album and then showPrivate voidPhonechoose (intResultCode, Intent data) { if(ResultCode = =RESULT_OK) {Contentresolver resolver=Getcontentresolver (); Uri Originaluri=Data.getdata (); Try{Bitmap photo=MediaStore.Images.Media.getBitmap (resolver, Originaluri); if(Photo! =NULL) {picture.setimagebitmap (photo); } } Catch(IOException e) {e.printstacktrace (); } }}
Friendship Link
Android basic----> camera and album calls