Similar to the function of selecting a QQ avatar, the user can choose to take pictures from the mobile phone or take pictures by himself, and then crop the image size. At that time, the user thought it was very practical, but he did not know how to implement it. I recently read the code written by my colleagues and read relevant information on the Internet. I found that the methods are the same. I have simply integrated them to implement basic functions, as for upload, there is no in-depth research.
:
The following is the part of the Code, which is extracted from the Internet and used as a tool class.
Configuration File: the layout is very simple. An imagebutton and a button can be clicked to achieve the image selection function. The specific implementation depends on the actual effect. ------------------------------------------------- androidmanifest. xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cogent.piccut" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".PicCutActivity" android:screenOrientation="portrait" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
----------------------------------------------- Java code:
Package COM. cogent. piccut; import Java. io. file; import Java. text. simpledateformat; import Java. util. date; import android. app. activity; import android. app. alertdialog; import android. content. dialoginterface; import android. content. intent; import android. graphics. bitmap; import android. graphics. drawable. bitmapdrawable; import android. graphics. drawable. drawable; import android.net. uri; import android. OS. bundle; Im Port android. OS. environment; import android. provider. mediastore; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. imagebutton; public class piccutactivity extends activity implements onclicklistener {private imagebutton img_btn; private button BTN; Private Static final int photo_request_takephoto = 1; // take a photo of Private Static final int P Hoto_request_gallery = 2; // select Private Static final int photo_request_cut = 3 from the album; // result // create a file named at the current time: file tempfile = new file (environment. getexternalstoragedirectory (), getphotofilename ();/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Init () ;}// initialize the private VO control. Id Init () {img_btn = (imagebutton) findviewbyid (R. id. img_btn); BTN = (button) findviewbyid (R. id. BTN); // Add the listening event img_btn.setonclicklistener (this) for imagebutton and button; BTN. setonclicklistener (this);} // Click Event @ override public void onclick (view v) {// todo auto-generated method stub switch (v. GETID () {case R. id. img_btn: showdialog (); break; case R. id. BTN: showdialog (); break ;}// method of the prompt dialog box private Vo Id showdialog () {New alertdialog. builder (this ). settitle ("avatar Settings "). setpositivebutton ("photo", new dialoginterface. onclicklistener () {@ override public void onclick (dialoginterface dialog, int which) {// todo auto-generated method stub dialog. dismiss (); // call the system's camera function intent = new intent (mediastore. action_image_capture); // specifies the storage path of the image after the camera is taken. putextra (mediastore. extra_output, Uri. fromfile (Te Mpfile); startactivityforresult (intent, photo_request_takephoto );}}). setnegativebutton ("album", new dialoginterface. onclicklistener () {@ override public void onclick (dialoginterface dialog, int which) {// todo auto-generated method stub dialog. dismiss (); intent = new intent (intent. action_pick, null); intent. setdataandtype (mediastore. images. media. external_content_uri, "image/*"); startactiv Ityforresult (intent, photo_request_gallery );}}). show () ;}@ override protected void onactivityresult (INT requestcode, int resultcode, intent data) {// todo auto-generated method stub switch (requestcode) {Case photo_request_takephoto: startphotozoom (URI. fromfile (tempfile), 150); break; Case photo_request_gallery: If (Data! = NULL) startphotozoom (data. getdata (), 150); break; Case photo_request_cut: If (Data! = NULL) setpictoview (data); break;} super. onactivityresult (requestcode, resultcode, data);} private void startphotozoom (URI Uri, int size) {intent = new intent ("com. android. camera. action. crop "); intent. setdataandtype (Uri, "image/*"); // If crop is set to true, the displayed view can be customized in the enabled intent. putextra ("crop", "true"); // aspectx aspecty is a width-high ratio intent. putextra ("aspectx", 1); intent. putextra ("aspecty ", 1); // outputx, outputy is the width and height intent of the cropped image. putextra ("outputx", size); intent. putextra ("outputy", size); intent. putextra ("Return-Data", true); startactivityforresult (intent, photo_request_cut);} // display the cropped image to private void setpictoview (intent picdata) on the UI) {bundle = picdata. getextras (); If (bundle! = NULL) {bitmap photo = bundle. getparcelable ("data"); drawable = new bitmapdrawable (photo); img_btn.setbackgrounddrawable (drawable) ;}// adjust it as the photo name private string getphotofilename () using the current system date () {date = new date (system. currenttimemillis (); simpledateformat dateformat = new simpledateformat ("'img '_ yyyymmdd_hhmmss"); Return dateformat. format (date) + ". jpg ";}}
Conclusion: The androi system comes with the image cropping function, which can be called for development. Many intent usage is more practical, but there are too many, when you need to use it, you can query the official documents or take a look at the official documents. Many codes are simple, but you still need to write them yourself better and understand them more deeply.