You think Avatar replacement is easy? Perhaps for users, in the micro-letter on the replacement of a avatar is just click on the avatar, choose to take photos or albums, cropping back. But for programmers, it's really hard to realize (a little match takes an afternoon to tidy up ~_~).
As the user uses, the implementation of the code is gradually expanded in the order of operation. The following figure:
The next major step is to explain the code:
1. Frame Selection photo album or photo
The simpler way is to use the Alertdialog pop-up option directly for the user to choose
public static void Showimagepickdialog (final activity activity) {
String title = "Choose how to Get Pictures";
string[] items = new string[]{"Photo", "album"};
New Alertdialog.builder (activity)
. Settitle (title)
. Setitems (items, new Dialoginterface.onclicklistener () {
@Override public
void OnClick (dialoginterface dialog, int which) {
Dialog.dismiss ();
Switch (which) {case
0:
//Choose Photo
Pickimagefromcamera (activity);
break;
Case 1:
//Select album
pickimagefromalbum (activity);
break;
Default: Break;}}}
). Show ();
}
2. Call System Camera
Before calling the system camera, we need to pass in the picture URI of the custom path, specifying that the picture of the photograph is stored under this URI, and if the GetData () is used to get the compressed bitmap data directly on the return
public static void Pickimagefromcamera (final activity activity) {
//Get URI of the specified path
Imageurifromcamera = Getimageuri ();
Intent Intent = new Intent ();
Intent.setaction (mediastore.action_image_capture);
Intent.putextra (Mediastore.extra_output,imageurifromcamera);
Activity.startactivityforresult (Intent,request_code_from_camera);
}
3. Open the System album
Call the system album directly, and then manipulate the returned data
public static void Pickimagefromalbum (final activity activity) {
//implicit invocation, may appear multiple choices
Intent Intent = new Intent ();
intent.setaction (intent.action_get_content);
Intent.settype ("image/*");
Activity.startactivityforresult (intent,request_code_from_album);
}
4. To the return picture URI to cut work
For the photo return, directly to the return picture to crop, and for the selection of album return, we need to first copy the original picture to our customized directory (to avoid the clipping operation on the quality of the original picture), and then the copied pictures to crop
@Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult (reques
Tcode, ResultCode, data);
Switch (requestcode) {case imageutils.request_code_from_album: {if (ResultCode = = result_canceled) {//Cancel operation
Return
Uri Imageuri = Data.getdata (); Imageutils.copyimageuri (This,imageuri); Copy Picture Imageutils.cropimageuri (this, Imageutils.getcurrenturi (), 200, 200);
Crop the picture break; Case Imageutils.request_code_from_camera: {if (ResultCode = = result_canceled) {//Cancel operation Imageutils.deleteim Ageuri (this, Imageutils.getcurrenturi ()); Delete URI} Imageutils.cropimageuri (this, Imageutils.getcurrenturi (), 200, 200);
Crop the picture break;
Case Imageutils.request_code_crop: {if (ResultCode = = result_canceled) {//cancel operation return;
Uri Imageuri = Imageutils.getcurrenturi ();
if (Imageuri!= null) {Imageview.setimageuri (Imageuri);//Show Picture} break; } defAult:break;
}
}
The above only gives some code, need complete code classmate can go to github download:
Https://github.com/ZhouCP/PhotoDemo
This is the entire content of this article, I hope to learn more about Android software programming help.