Android to take photos, select photo albums and crop features _android

Source: Internet
Author: User

Get the picture from a photo or photo album, crop it, and then display the picture on the ImageView.
  Of course can also upload to the server (most of the project is uploaded to the server), referring to the online data and combined with the actual situation of the project,
  tested a variety of mobile phones temporarily did not find serious problems. The code has a comment that directly pastes the code:

public class Uploadpicactivity extends activity implements View.onclicklistener {private Button take_photo_btn;
 Private Button select_photo_btn;
 Private ImageView Photo_iv;
 Take pictures with camera get picture public static final int take_photo_code = 1;
 Use pictures in albums public static final int select_pic_code = 2;
 Picture clipping private static final int photo_crop_code = 3;
 Defines the URI private URI Photouri of the picture;

 Picture file path Private String Picpath;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.activity_upload_pic);
 Initviews ();
  private void Initviews () {this.take_photo_btn = (Button) Findviewbyid (R.ID.TAKE_PHOTO_BTN);
  This.take_photo_btn.setOnClickListener (this);
  THIS.SELECT_PHOTO_BTN = (Button) Findviewbyid (R.ID.SELECT_PHOTO_BTN);
  This.select_photo_btn.setOnClickListener (this);
 This.photo_iv = (ImageView) Findviewbyid (R.ID.PHOTO_IV); @Override public void OnClick (view view) {switch (View.getid ()) {//Photo
   Case R.id.take_photo_btn:pictytakephoto ();
   Break
    Select the Gallery case R.id.select_photo_btn:pickphoto ();
  Break /** * Photo capture photo/private void Pictytakephoto () {//Determine if SD card exists String sdstate = Environment.getexternalstorag
  Estate (); if (Sdstate.equals (environment.media_mounted)) {Intent Intent = new Intent (mediastore.action_image_capture);//" Android.media.action.IMAGE_CAPTURE "/*** * Use a camera to take a picture and the pictures will be stored in an album. The advantage of this way is: Get the picture is the original image after the photo, * If not practical contentvalues store photo path, after taking pictures for thumbnails may not be clear * * * contentvalues values = new Contentvalues (
   );
   Photouri = Getcontentresolver (). Insert (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
   Intent.putextra (Android.provider.MediaStore.EXTRA_OUTPUT, Photouri);
  Startactivityforresult (Intent, Take_photo_code);
  else {Toast.maketext (this, "memory card does not exist", Toast.length_long). Show ();
  }/*** * Take picture from album/private void Pickphoto () {Intent Intent = new Intent (Intent.action_pick, NULL); Intent.setdataanDtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
 Startactivityforresult (Intent, Select_pic_code); @Override public void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult (reque
  Stcode, ResultCode, data);  if (ResultCode = = ACTIVITY.RESULT_OK) {//photos taken from the album, some mobile phones have unusual conditions, please note if (Requestcode = = Select_pic_code) {if (null!=
     Data && null!= data.getdata ()) {Photouri = Data.getdata ();
     Picpath = Uritofilepath (Photouri);
    Startphotozoom (Photouri, Photo_crop_code);
    else {Toast.maketext (this, picture selection failed, Toast.length_long). Show ();
    } else if (Requestcode = = Take_photo_code) {string[] Pojo = {MediaStore.Images.Media.DATA};
    Cursor Cursor = Managedquery (Photouri, pojo, NULL, NULL, NULL);
     if (cursor!= null) {int columnindex = Cursor.getcolumnindexorthrow (pojo[0]);
     Cursor.movetofirst ();
     Picpath = cursor.getstring (columnindex); if (Build.VERSION.SDK_INT &LT
     ) {cursor.close ();
     } if (Picpath!= null) {Photouri = Uri.fromfile (new File (Picpath));
    Startphotozoom (Photouri, Photo_crop_code);
    else {Toast.maketext (this, picture selection failed, Toast.length_long). Show (); } else if (Requestcode = = Photo_crop_code) {if (Photouri!= null) {Bitmap Bitmap = Bitmapfactory.decodefil
     E (Picpath);
     if (bitmap!= null) {///Here you can upload the picture to the server Operation Photo_iv.setimagebitmap (bitmap); /** * @param * @description cropping picture * @author LDM * @time 2016/11/30 15:19/private void S
  Tartphotozoom (URI uri, int reque_code_crop) {Intent Intent = new Intent ("Com.android.camera.action.CROP");
  Intent.setdataandtype (URI, "image/*");
  Crop=true is set to set the view trimmed Intent.putextra ("crop", "true") displayed in the open intent.
  Go to the black side Intent.putextra ("scale", true);
  Intent.putextra ("scaleupifneeded", true);
  Aspectx Aspecty is a broadly high proportion, modified according to its own situation Intent.putextra ("Aspectx", 3); Intent.putextRA ("Aspecty", 2);
  Outputx Outputy is a cropped picture with a wide-high pixel Intent.putextra ("Outputx", 600);
  Intent.putextra ("Outputy", 400);
  Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ());
  Remove Face recognition function Intent.putextra ("Nofacedetection", true);
  Sets the URI Intent.putextra (Mediastore.extra_output, URI) returned;
  Set to not return data Intent.putextra ("Return-data", false);
 Startactivityforresult (Intent, reque_code_crop); /** * @param * @description converts URIs to file paths * @author LDM * @time 2016/11/30 15:22/Private String Uritofilep
  Ath (URI uri) {//Get Picture data string[] proj = {MediaStore.Images.Media.DATA};
  Query Cursor Cursor = Managedquery (URI, proj, NULL, NULL, NULL);
  Gets the index value of the user-selected picture int image_index = Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA);
  Cursor.movetofirst ();
 Returns the picture path return cursor.getstring (Image_index);
 }
}

Layout file Length:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" Vertica
  L "android:padding=" 16DP "> <button android:id=" @+id/take_photo_btn "android:layout_width=" Match_parent " android:layout_height= "Wrap_content" android:layout_margintop= "20DP" android:gravity= "center" android:text= "Photo" an Droid:textsize= "16sp"/> <button android:id= "@+id/select_photo_btn" android:layout_width= "Match_parent" Andro id:layout_height= "Wrap_content" android:layout_margintop= "20DP" android:gravity= "Center" android:text= "Select Picture" Andro Id:textsize= "16sp"/> <imageview android:id= "@+id/photo_iv" android:layout_width= "Wrap_content" android:layou t_height= "Wrap_content" android:layout_gravity= "Center_horizontal" android:layout_margintop= "20DP"/> </
 Linearlayout>

Finally, don't forget to add uploadpicactivity and permissions in Androidmanifest.xml:

<uses-permission android:name= "Android.permission.CAMERA"/>
<uses-permission android:name= " Android.permission.WRITE_EXTERNAL_STORAGE "/>
<uses-permission 
 android:name=" Android.permission.MOUNT_UNMOUNT_FILESYSTEMS "/>

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.

Related Article

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.