Android photos, albums get pictures, crop pictures

Source: Internet
Author: User
Tags dateformat

Recently doing business-to-business projects, most of the images from the user upload themselves;
Due to the different size of Android, user camera, photo album is also grotesque;
So it is necessary to do a clipping of the picture before uploading it!

Here is the demo of scaling the image proportionally

Resource File Activity_main.xml

<Relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"    Xmlns:tools="Http://schemas.android.com/tools"    Android:layout_width="Match_parent"    Android:layout_height="Match_parent"    Android:paddingbottom="@dimen/activity_vertical_margin"    Android:paddingleft="@dimen/activity_horizontal_margin"    Android:paddingright="@dimen/activity_horizontal_margin"    Android:paddingtop="@dimen/activity_vertical_margin"    Tools:context="Com.example.croppicturedemo.MainActivity">    <linearlayoutandroid:layout_width="Match_parent"android:layout_height ="Match_parent"android:orientation="vertical">                                 <buttonandroid:id="@+id/btn"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content "android:text=" Select Picture " />                                                        <ImageViewandroid:id= "@+id/imageview"android:layout_width=" Match_parent "android:layout_height=" match_parent "/>                                         </linearlayout></relativelayout>

Mainactivity

 Public  class mainactivity extends Activity implements Onclicklistener  {        PrivateImageView ImageView;PrivateButton btn;Private Static Final intPhoto_request_takephoto =1;//Photo        Private Static Final intPhoto_request_gallery =2;//Select from the album        Private Static Final intPhoto_request_cut =3;//Results        //Create a file with the current time nameFile tempfile =NewFile (Environment.getexternalstoragedirectory (), Getphotofilename ());/** Called when the activity is first created. * /        @Override         Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);            Setcontentview (R.layout.activity_main);        Init (); }//Initialize control        Private void Init() {ImageView = (ImageView) Findviewbyid (R.id.imageview); BTN = (Button) Findviewbyid (R.ID.BTN);//Add listener events for ImageButton and buttonImageview.setonclicklistener ( This); Btn.setonclicklistener ( This); }//Click events        @Override         Public void OnClick(View v) {//TODO auto-generated method stub            Switch(V.getid ()) { CaseR.id.imageview:showdialog (); Break; CaseR.id.btn:showdialog (); Break; }        }//Prompt dialog box method        Private void ShowDialog() {NewAlertdialog.builder ( This). Settitle ("Avatar Settings"). Setpositivebutton ("Take photos",NewDialoginterface.onclicklistener () {@Override                         Public void OnClick(Dialoginterface Dialog,intwhich) {//TODO auto-generated method stubDialog.dismiss ();//Recall the camera function of the systemIntent Intent =NewIntent (mediastore.action_image_capture);//Specify the storage path of the photo after the camera is calledIntent.putextra (Mediastore.extra_output,uri.fromfile (tempfile));                        Startactivityforresult (Intent, Photo_request_takephoto); }}). Setnegativebutton ("Albums",NewDialoginterface.onclicklistener () {@Override                         Public void OnClick(Dialoginterface Dialog,intwhich) {//TODO auto-generated method stubDialog.dismiss (); Intent Intent =NewIntent (Intent.action_pick,NULL); Intent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");                        Startactivityforresult (Intent, photo_request_gallery);        }}). Show (); }@Override        protected void Onactivityresult(intRequestcode,intResultCode, Intent data) {//TODO auto-generated method stub            Switch(Requestcode) { CasePhoto_request_takephoto:startphotozoom (Uri.fromfile (tempfile), Max); Break; CasePhoto_request_gallery:if(Data! =NULL) Startphotozoom (Data.getdata (), Max); Break; CasePhoto_request_cut:if(Data! =NULL) Setpictoview (data); Break; }Super. Onactivityresult (Requestcode, ResultCode, data); }Private void Startphotozoom(Uri Uri,intSize) {Intent Intent =NewIntent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI,"image/*");//Crop is true to set the view set in the open intent to be clippedIntent.putextra ("Crop","true");//Aspectx aspecty is the ratio of width to heightIntent.putextra ("Aspectx",1); Intent.putextra ("Aspecty",1);//Outputx,outputy is the width height of the cropped pictureIntent.putextra ("Outputx", size); Intent.putextra ("Outputy", size); Intent.putextra ("Scale",true); Intent.putextra ("Return-data",true);        Startactivityforresult (Intent, photo_request_cut); }//The cropped picture is displayed to the UI interface        Private void Setpictoview(Intent Picdata) {Bundle bundle = Picdata.getextras ();if(Bundle! =NULL) {Bitmap photo = Bundle.getparcelable ("Data"); Drawable drawable =NewBitmapdrawable (photo);            Imageview.setbackgrounddrawable (drawable); }        }//Use the system's current date to adjust the name of the photo        PrivateStringGetphotofilename() {Date date =NewDate (System.currenttimemillis ()); SimpleDateFormat DateFormat =NewSimpleDateFormat ("' IMG ' _yyyymmdd_hhmmss");returnDateformat.format (date) +". jpg"; }}

Demo: Click to download

Android photos, albums get pictures, crop pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.