On the photo interception and photo album interception, read a lot of information on the Internet, they have collated a more detailed, for people in need of reference
1 photo shoot
The principle is to call the system through the intent camera, photographed in the callback operation, the successful capture of the picture according to the URI call the system's crop page, cropping is also in the callback processing, displayed in the page ImageView.
2 albums Select pictures
The principle is also through the intent to open the picture of the system, the user chooses to complete in the callback according to the picture's URI to call the system's clipping page, ditto.
First, define a few constants
public static final int take_photo = 1;
public static final int crop_photo = 2;
public static final int action_choose=3;
Private Uri Imageuri; Picture path
private String filename;//Picture name
And then handle the event in the photo and photo album selection button.
@Override public void OnClick (View v) {switch (V.getid ()) {case R.ID.BT1://Photo Capture Simpledatefor
Mat format = new SimpleDateFormat ("Yyyymmddhhmmss");
Date date = new Date (System.currenttimemillis ());
filename = Format.format (date);
File path = environment.getexternalstoragepublicdirectory (ENVIRONMENT.DIRECTORY_DCIM);
File Outputimage = new file (path, filename + ". jpg");
try {if (outputimage.exists ()) {outputimage.delete ();
} outputimage.createnewfile ();
catch (IOException e) {e.printstacktrace ();
//Convert the file object to a Uri and start the camera program Imageuri = Uri.fromfile (outputimage); Intent Intent = new Intent (mediastore.action_image_capture); Photographic Intent.putextra (Mediastore.extra_output, Imageuri); Specify picture output address Startactivityforresult (intent, Take_photo);
Start the photo break; Case R.ID.BT2://album Selection Intent Intent2 = new Intent (Intent.action_pick);
Intent2.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
Startactivityforresult (Intent2,action_choose);
Break }
}
The most important execution in a callback
The method called when using Startactivityforresult returns a result
@Override
protected void onactivityresult (int requestcode, int ResultCode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
if (RESULTCODE!=RESULT_OK) {
Toast.maketext (this, "canceled", Toast.length_short). Show ();
return;
}
Switch (requestcode) {case
Take_photo: //Photo callback
Cropimage ();
break;
Case Action_choose: //Select the callback of the photo
Imageuri = Data.getdata ();
Cropimage ();
break;
Case Crop_photo: //The callback Showpic () of the cropped photo
;
break;
Default: Break
;
}
How to crop a picture
/**
* Crop picture
**/
private void Cropimage () {
Intent Intent = new Intent ("Com.android.camera.action.CROP") ; Trim
Intent.setdataandtype (Imageuri, "image/*");
Intent.putextra ("scale", true);
Set wide and high proportions
of Intent.putextra ("Aspectx", 1);
Intent.putextra ("Aspecty", 1);
Set cropped picture width high
intent.putextra ("Outputx",);
Intent.putextra ("Outputy",);
Intent.putextra (Mediastore.extra_output, Imageuri);
Toast.maketext (Mainactivity.this, "trim picture", Toast.length_short). Show ();
Broadcast Refresh album
Intent INTENTBC = new Intent (intent.action_media_scanner_scan_file);
Intentbc.setdata (Imageuri);
This.sendbroadcast (INTENTBC);
Startactivityforresult (Intent, Crop_photo); Set crop parameters to display picture to ImageView
}
The process is relatively simple, there may be some judgments where there is no treatment, if there is a wrong place to hope that a master can help improve, thank you ~ ~
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.