We often use the upload avatar, or send a post to choose the local image upload function. This is very common.
Today, I studied the needs of the app. Share now.
In fact, whether by photo or album selection will be used to intent
This is a handy tool that the system provides to us to call system methods!
First of all, we need to design how we want to call the system to take pictures or choose a picture method
We can click on the avatar or a button and then pop up a dialog box to let the user
Choose whether to take a photo or select a picture (e.g.)
.
How do you write this dialog box? By Alertdialog (We call this method the name dialog):
//the avatar Operation Private voiddialog () {Alertdialog.builder Builder=NewAlertdialog.builder (mainactivity. This); Builder.settitle (Prompted); Builder.setmessage ("Please choose to change the avatar mode"); //call camera to take picturesBuilder.setpositivebutton ("Take pictures",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialoginterface,inti) {Takephoto (); } }); //take photos from the albumBuilder.setnegativebutton ("album",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialoginterface,inti) {Pickphoto (); } }); Builder.create (). Show (); }
Then we will implement the photo and photo album selection function (via intent):
/*** Take photos to get pictures*/ Private voidTakephoto () {//before performing a photo shoot, you should first determine if the SD card existsString sdstate =environment.getexternalstoragestate (); if(Sdstate.equals (environment.media_mounted)) {Intent Intent=NewIntent (mediastore.action_image_capture);//"Android.media.action.IMAGE_CAPTURE" /*** * need to explain, the following operation using the camera to take pictures, the pictures will be stored in the photo album * Here is the advantage of using this method is to obtain the picture is taken after the original photo * if not practical conten Tvalues the photo path, the picture obtained after taking pictures is not clear for the thumbnail image*/contentvalues Values=Newcontentvalues (); Photouri= This. Getcontentresolver (). Insert (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); Intent.putextra (Mediastore.extra_output, Photouri); //Intent.putextra ("uri", Photouri); /**-----------------*/Startactivityforresult (Intent, Select_pic_by_tack_photo); } Else{Toast.maketext ( This, "Memory card does not exist", Toast.length_long). Show (); } } /*** * Take pictures from the album*/ Private voidPickphoto () {//androidimagepicker.getinstance (). Setselectmode (AndroidImagePicker.Select_Mode.MODE_MULTI);Intent Intent =NewIntent (Intent.action_pick,NULL); //Filter all the pictures by intentIntent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); Startactivityforresult (Intent, req_image); }
Intent Intent = new Intent (mediastore.action_image_capture), the ability to call the system to take pictures
By overriding the Onactivityresult method to get through
Startactivityforresult () The message (mainly the URI of the picture taken).
Get the URI:
Uri uri = Data.getdata ();
Once you get the URI, you can set the background for the corresponding control. For example, to set a background for a page:
BT = MediaStore.Images.Media.getBitmap (this. Getcontentresolver (), URI); Im.setimagebitmap (BT);
BT is the bitmap type IM is the ImageView type and has already bound the control.
The parameters of the Startactivityforresult () are included (the Intent data change parameter mainly holds the selected or photographed URI)
Intent Intent = new Intent (Intent.action_pick, null); Intent.setdataandtype (MediaStore.Images.Media.EXTERNAL_ Content_uri, "image/*");
These two sentences are the method of invoking the system picture selection. Also pass parameters via Startactivityforresult ()
Startactivityforresult (Intent, int.)
This method is compatible with Onactivityresult ()
Its first parameter is used to pass the INTETN information, the second parameter is its own defined constant used to uniquely identify his intent
The second parameter of Onactivityresult () is the unique identifier. The third parameter is the intent information.
@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { Super. Onactivityresult (Requestcode, ResultCode, data); if(ResultCode = = Activity.result_ok && Requestcode = =Select_pic_by_tack_photo) { Try{Bitmap bt= MediaStore.Images.Media.getBitmap ( This. Getcontentresolver (), Data.getdata ());
Mtext1.settext (Data.getdata (). toString ()); Im.setimagebitmap (BT); } Catch(IOException e) {e.printstacktrace (); } }Else{Bitmap bt=NULL; Try{URI Uri=Data.getdata (); Mtext1.settext (Uri.tostring ()); BT= MediaStore.Images.Media.getBitmap ( This. Getcontentresolver (), URI); Im.setimagebitmap (BT); } Catch(IOException e) {e.printstacktrace (); } im.setimagebitmap (BT); }
That's it. Information about the specific parameters of Onactivityresult () can be self-checking documents or Baidu.
Android take photos or get pictures from the album implementation