Using Java code to implement picture clipping in Android _android

Source: Internet
Author: User

Objective
Android applications often encounter the need to upload photo albums, here is a record of how to choose and cut photo albums.

Album Selection picture
1. Activate the photo album or file Manager to get the photo with the following code:

 private static final int take_picture_from_album = 1; 
 private void Takepicturefromalbum () { 
  Intent Intent = new Intent (intent.action_get_content); 
  Intent.settype ("image/*"); 
  Startactivityforresult (Intent, take_picture_from_album); 
 } 

2. Through the Startacitivityforresult () method, we can get the URI of the picture in Onacitivityresult (), and then pass the URI of the picture to the picture crop function, the code is as follows:

 @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode = = Activit Y.RESULT_OK) {switch (requestcode) {case take_picture_from_album:if (data!= null) {Croppicturefrom 
    Album (Data.getdata ()); 
   } break; Case PICTURE_CROP_CODE:IF (data!= null) {//This is the Bitmap of the cropped picture Bitmap Bitmap = Data.getparcelableextra ("Da 
    Ta "); 
   } break; 
   Default:break; 
  
 }} private static final int take_picture_from_album = 1; 
  private void Takepicturefromalbum () {Intent Intent = new Intent (intent.action_get_content); 
  Intent.settype ("image/*"); 
 Startactivityforresult (Intent, take_picture_from_album); 
  
 private static final int picture_crop_code = 2; 
  private void Croppicturefromalbum (Uri pictureuri) {Intent Intent = new Intent ("Com.android.camera.action.CROP"); 
  Intent.setdataandtype (Pictureuri, "image/*"); 
  Intent.putextra ("Crop", "true"); Crop boxProportional Intent.putextra ("Aspectx", 1); 
  
  Intent.putextra ("Aspecty", 2); 
  After cropping, enter the size of the picture Intent.putextra ("Outputx", 300); 
  
  Intent.putextra ("Outputy", 300); 
  
  Remove face recognition Intent.putextra ("Nofacedetection", true); 
  
  Intent.putextra ("Return-data", true); 
 Startactivityforresult (Intent, Picture_crop_code); 

 }

3. In Onactivityresult, Data.getparcelableextra ("data") can get the bitmap after the picture is cropped, then you can assign bitmap to the picture control.

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.