Android Select local picture and crop

Source: Internet
Author: User

This article uses the Android native selection and cropping feature. Here's the code:

public class Pickandcropactivity extends appcompatactivity implements view.onclicklistener{public static final int REQ    uest_code_pick_img = 1;    public static final int request_code_crop_img = 2;    /** * ImageView Show selected pictures and cropped images */Private ImageView img;    /** * Select the resulting picture URI */private URI MUri;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_pick_and_crop);    img = (ImageView) Findviewbyid (r.id.img);                } @Override public void OnClick (View v) {switch (V.getid ()) {r.id.pick:                Startactivityforresult (Intentutils.generatepickimgintent (), request_code_pick_img);            Break Case R.id.crop:startactivityforresult (Intentutils.generatecropimgintent (MUri, Max), request_code_crop_img)                ;        Break }} @Override protected void Onactivityresult (iNT Requestcode, int resultcode, Intent data) {switch (requestcode) {case Request_code_pick_i                Mg:if (ResultCode! = RESULT_OK | | data = NULL) break;                MUri = Data.getdata ();                Contentresolver CR = This.getcontentresolver ();                    try {Bitmap Bitmap = Bitmapfactory.decodestream (Cr.openinputstream (MUri));                Img.setimagebitmap (bitmap);                } catch (FileNotFoundException e) {e.printstacktrace ();            } break;                Case Request_code_crop_img:if (ResultCode! = RESULT_OK | | data = NULL) break;                Bitmap Bmap = Data.getparcelableextra ("Data");                Img.setimagebitmap (BMAP);        Break    } super.onactivityresult (Requestcode, ResultCode, data); }}

The Intentutils class used in the middle:

public class intentutils{    /**     * Constructs a select picture of the Intent     * * @return */public    static Intent Generatepickimgintent ()    {        Intent Intent = new Intent (Intent.action_pick);        Intent.settype ("image/*");        return intent;    }    /**     * Constructs a cropped picture of intent     *     * @param uri     * @param size     * @return    */public static Intent generatecropimgintent (URI uri, int size)    {        Intent Intent = new Intent ("Com.android.camera.action.CROP ");        Muri is a picture URI that has been selected        Intent.setdataandtype (URI, "image/*");        Intent.putextra ("Crop", "true");        Cropping frame proportional        intent.putextra ("Aspectx", 1);        Intent.putextra ("Aspecty", 1);        Output picture size        Intent.putextra ("Outputx", size);        Intent.putextra ("Outputy", size);        Intent.putextra ("Return-data", true);        return intent;}    }

Android Select local picture and crop

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.