android-Call system Contentprivder get a single picture to achieve cut-and-do avatar and source code download

Source: Internet
Author: User

First of all, describe the features of this small project :
1, call the system registration application to get a single picture
2. Easy to cut a single picture to make a specific size of the avatar picture
3, to obtain the results of the image analysis, the use of three ways.

First Look at:
Open the app and enter the note screen. Click Get Picture button

Call the system's picture selector, select the Gallery,

Click on the picture. Back to get to picture

Click the Cut Picture button to call the system's picture cutting tool

Gets the picture after the cut

See the code below for more details:

 Public  class mainactivity extends Activity implements Onclicklistener {    PrivateTextView Backtx,suretx;//A return key, a OK key    PrivateImageView ImageView;//Show ImageView of the acquired picture    PrivateButton Getpic,getpic2cut;//two buttons, respectively, to obtain the picture, cut the picture    Private Final intREQUEST = One;//Get the request value of the picture    Private Final intHead_image_zoom = A;//Picture cut request Value    PrivateURI Uri;PrivateString TAG = MainActivity.class.getSimpleName ();@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Requestwindowfeature (Window.feature_no_title);        Setcontentview (R.layout.main);        Init ();    Setclicklistener (); }Private void Init() {BACKTX = (TextView) Findviewbyid (r.id.cancle);        Suretx = (TextView) Findviewbyid (R.id.ok);        ImageView = (ImageView) Findviewbyid (r.id.image);        Getpic = (Button) Findviewbyid (r.id.get_pic);    Getpic2cut = (Button) Findviewbyid (r.id.get_pic_to_cut); }Private void Setclicklistener() {Backtx.setonclicklistener ( This); Suretx.setonclicklistener ( This); Getpic.setonclicklistener ( This); Getpic2cut.setonclicklistener ( This); }//Call the system to get a picture of the method, very easy    Private void LoadData() {Intent Intent =NewIntent (intent.action_get_content); Intent.settype ("image/*");    Startactivityforresult (Intent, REQUEST); }@Override    protected void OnDestroy() {Super. OnDestroy (); }@Override     Public void OnClick(View v) {Switch(V.getid ()) { CaseR.id.cancle:finish (); Break; CaseR.id.ok:toast.maketext (mainactivity. This,"The picture has been acquired", Toast.length_short). Show (); Break; CaseR.id.get_pic:loaddata (); Break; CaseR.id.get_pic_to_cut:startphotozoom (URI); Break; }    }@SuppressWarnings("Deprecation")@Override    protected void Onactivityresult(intRequestcode,intResultCode, Intent data) {Super. Onactivityresult (Requestcode, ResultCode, data);if(Requestcode = = REQUEST && ResultCode = = RESULT_OK) {uri = Data.getdata ();/** * Method One: * Directly using the ImageView method to set the image address URI can be. * Just this way when the picture is large, easy appears oom abnormally. Use with caution!

*/Imageview.setimageuri (URI);/** * Method Two: * Query based on the URI obtained, zoom after the picture is obtained, no oom exception occurs */cursor cursor = managedquery (URI,NULL,NULL,NULL,NULL);if(Cursor! =NULL) {LOG.I (TAG,"number of records ="+cursor.getcount ());if(Cursor.movetofirst ()) {intindex = Cursor.getcolumnindexorthrow (media.data); String path = cursor.getstring (index); Bitmapfactory.options Options =NewBitmapfactory.options (); Options.insamplesize =2; Bitmap Bitmap = bitmapfactory.decodefile (path, options); Imageview.setimagebitmap (bitmap); } }Else{Toast.maketext (mainactivity). This,"Unable to get picture data", Toast.length_short). Show (); }/** * Method Three: * Because managedquery this method is no longer recommended * So use Cursorloader class complete query operation */Cursorloader Cursorloader =NewCursorloader (mainactivity. ThisUriNULL,NULL,NULL,NULL); Cursor Cursor2 = Cursorloader.loadinbackground ();if(Cursor2! =NULL) {LOG.I (TAG,"number of records ="+cursor2.getcount ());if(Cursor2.movetofirst ()) {intindex = Cursor2.getcolumnindexorthrow (media.data); String path = cursor2.getstring (index); Bitmapfactory.options Options =NewBitmapfactory.options (); Options.insamplesize =2; Bitmap Bitmap = bitmapfactory.decodefile (path, options); Imageview.setimagebitmap (bitmap); } }Else{Toast.maketext (mainactivity). This,"Unable to get picture data", Toast.length_short). Show (); } }Else if(Requestcode = = Head_image_zoom && ResultCode = = RESULT_OK) {Bundle bundle = Data.getextras ();if(Bundle! =NULL) {Bitmap Bitmap = bundle.getparcelable ("Data"); Imageview.setimagebitmap (bitmap); }Else{Toast.maketext (mainactivity). This,"Picture clipping failed", Toast.length_short). Show (); } }Else if(Requestcode = = head_image_zoom) {Toast.maketext (mainactivity). This,"Picture Cancel Cut", Toast.length_short). Show (); } }/** * Call the system's program to take pictures of the cut operation * Get a cut picture of a specified size for easy avatar * @param URI */ Private void Startphotozoom(URI Uri) {Intent Intent =NewIntent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI,"image/*");//The following this crop=true is set to set the display in the open intent view can be croppedIntent.putextra ("Crop","true");//Aspectx aspecty is the ratio of width to heightIntent.putextra ("Aspectx",1); Intent.putextra ("Aspecty",1);//Outputx outputy is cropped picture width highIntent.putextra ("Outputx", the); Intent.putextra ("Outputy", the); Intent.putextra ("Return-data",true); Startactivityforresult (Intent, head_image_zoom); }}

The above code gets the return value of the process, using three methods, the detailed implementation of the time is only one way to do. The third way is recommended.
It is worth mentioning that. This small project needs to add a privilege value to the configuration file:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Error without this permission!

This small project is useful. Most of the apps have access to the user's avatar, the user's avatar setting operation, so we can participate in the test, if there is any wrong place, please positive message exchange, common progress. Thank you! Handshake

SOURCE download

Android-Call the system Contentprivder get a single picture to achieve cut-and-do avatar and source code download

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.