Selecting a picture in Android is a very common feature, and the source of the image is usually obtained from the camera and obtained from the album Two.
First, write a simple selection button and a imageview that can display the image.
<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/bt_picture"  android:layout_width  = "match_parent"  android:layout_height  = "wrap_content"  android:text  = "get Picture" />     <ImageViewandroid:id= "@+id/picture"android:layout_width="Wrap_ Content "android:layout_height="wrap_content "android:layout_gravity=" Center _horizontal " />                                </linearlayout>
The next step is to implement the logic
    private String[] takePhotoitems;// 获取照片的操纵选项    publicstaticfinalint1;    publicstaticfinalint2;    private Button getPicture;    private ImageView picture;    private Uri imageUri;
Set the button's Click event
@Override     Public void OnClick(View v) {Takephotoitems =NewString[] {"Select local picture","Take photos"}; Imageuri = Getimageuri ();NewAlertdialog.builder ( This). Settitle ("Set Avatar"). Setitems (Takephotoitems,NewDialoginterface.onclicklistener () {@Override                             Public void OnClick(Dialoginterface Dialog,intwhich) {Switch(which) { Case 0: Getimageuri (); Intent Intent =NewIntent (intent.action_get_content); Intent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*"); Intent.settype ("image/*"); Intent.putextra ("Crop","true"); Intent.putextra ("Scale",true);                                    Intent.putextra (Mediastore.extra_output, Imageuri); Startactivityforresult (Intent, Crop_photo); Break; Case 1://PhotoGetimageuri ();if(Ishassdcard ()) {Intent takephotointent =NewIntent ("Android.media.action.IMAGE_CAPTURE");                                                Takephotointent.putextra (Mediastore.extra_output,                                        Imageuri);                                    Startactivityforresult (Takephotointent, Take_photo); }Else{Toast.maketext (mainactivity). This,"SD Card not detected", Toast.length_long). Show (); } Break; }}). Setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () {@Override                     Public void OnClick(Dialoginterface Dialog,intwhich) {Dialog.dismiss ();    }}). Show (); }
Two tool methods used
 /** * Get Picture Path */ private  Uri getimageuri  () {File outputimage = new  File (Environment.getexternalstoragedirectory (),  "Te        Mpimage.jpg "); if  (Outputimage.exists ())        {Outputimage.delete ();        } try  {outputimage.createnewfile ();        } catch  (IOException e) {e.printstacktrace ();    } return  uri.fromfile (outputimage); };
    /**     * 判断sd卡是否存在     */    publicstaticbooleanisHasSdcard() {        String state = Environment.getExternalStorageState();        if (state.equals(Environment.MEDIA_MOUNTED)) {            returntrue;        else {            returnfalse;        }    }
Crop after getting to the picture
    @Override    protected void Onactivityresult(intRequestcode,intResultCode, Intent data) {Switch(Requestcode) { CaseTake_photo:if(ResultCode = = RESULT_OK) {Intent Intent =NewIntent ("Com.android.camera.action.CROP"); Intent.setdataandtype (Imageuri,"image/*"); Intent.putextra ("Scale",true);                Intent.putextra (Mediastore.extra_output, Imageuri);            Startactivityforresult (Intent, Crop_photo); } Break; CaseCrop_photo:if(ResultCode = = RESULT_OK) {Try{Bitmap Bitmap = bitmapfactory. Decodestream (Getcontentresolver (). openinput                    Stream (Imageuri));                Picture.setimagebitmap (bitmap); }Catch(FileNotFoundException e)                {E.printstacktrace (); }            } Break;default: Break; }    }
 
Simple implementation in Android select picture and Crop