Android phone take photos or choose Gallery image as Avatar _android

Source: Internet
Author: User
Package Zhangpgil.photo;
Import Java.io.File;
Import android.support.v7.app.ActionBarActivity;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.ImageView;
Import Android.widget.Toast;
Import android.content.Intent;
Import Android.graphics.Bitmap;
Import Android.net.Uri;
Import Android.os.Bundle;
Import android.os.Environment;
 
Import Android.provider.MediaStore; public class Mainactivity extends actionbaractivity {* * * Avatar File/private static final String Image_file_name = "Temp
 
  _head_image.jpg ";
  /* Request Identification code */private static final int code_gallery_request = 0xa0;
  private static final int code_camera_request = 0XA1;
 
  private static final int code_result_request = 0XA2; After cropping the picture's width (x) and High (Y), the square of the 480. (Generate bitmap seems to be an error sometimes?)
  Try to make the size smaller. private static int output_x = 480;
 
  private static int output_y = 480;
 
  Private ImageView headimage = null; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstanceState);
 
    Setcontentview (R.layout.activity_main);
 
    Headimage = (ImageView) Findviewbyid (R.id.imageview);
    Button buttonlocal = (button) Findviewbyid (r.id.buttonlocal);
        Buttonlocal.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {
      Choseheadimagefromgallery ();
 
    }
    });
    Button Buttoncamera = (button) Findviewbyid (R.id.buttoncamera);
        Buttoncamera.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {
      Choseheadimagefromcameracapture ();
  }
    });
    //Select Picture from local album as Avatar private void Choseheadimagefromgallery () {Intent intentfromgallery = new Intent ();
    Sets the file type Intentfromgallery.settype ("image/*");
    Intentfromgallery.setaction (intent.action_get_content);
  Startactivityforresult (Intentfromgallery, code_gallery_request); //Start mobile camera take photos as Avatar private void Choseheadimagefromcameracapture () {Intent Intentfromcapture = new Intent (mediastore.action_image_capture); Determine if the memory card is available and store the photo file if (Hassdcard ()) {Intentfromcapture.putextra (Mediastore.extra_output, Uri). Fromfi
    Le (New File (Environment. getExternalStorageDirectory (), image_file_name));
  } startactivityforresult (Intentfromcapture, code_camera_request); @Override protected void Onactivityresult (int requestcode, int resultcode, Intent Intent) {//user did not have 
      Effect setting operation, return if (ResultCode = = result_canceled) {Toast.maketext (Getapplication (), Cancel, Toast.length_long). Show ();
    Return
      Switch (requestcode) {case Code_gallery_request:croprawphoto (Intent.getdata ());
 
    Break Case Code_camera_request:if (Hassdcard ()) {File Tempfile = new file (environment.getexternalst
        Oragedirectory (), image_file_name);
      Croprawphoto (Uri.fromfile (tempfile)); else {Toast.maketext (getapplication (), "No sdcard!", Toast.length_long). Show ();
 
    } break;
      Case Code_result_request:if (Intent!= null) {Setimagetoheadview (intent);
    } break;
  } super.onactivityresult (Requestcode, ResultCode, intent); /** * Crop the original picture */public void Croprawphoto (Uri uri) {Intent Intent = new Intent ("Com.android.camera.ac tion.
    Crop ");
 
    Intent.setdataandtype (URI, "image/*");
 
    Sets the cropping Intent.putextra ("crop", "true");
    Aspectx, Aspecty: Wide-high ratio Intent.putextra ("Aspectx", 1);
 
    Intent.putextra ("Aspecty", 1);
    OUTPUTX, Outputy: Cut the picture wide high Intent.putextra ("Outputx", output_x);
    Intent.putextra ("Outputy", output_y);
 
    Intent.putextra ("Return-data", true);
  Startactivityforresult (Intent, code_result_request); /** * Extracts the image data after saving the cropping, and sets the Avatar part view/private void Setimagetoheadview (Intent Intent) {Bundle extras = int
    Ent.getextras (); if (extras!= null) {BITMAP photo = extras.getparcelable ("Data");
    Headimage.setimagebitmap (photo); }/** * Check whether the device exists SDcard tool method/public static Boolean Hassdcard () {String state = Environment.getexter
    Nalstoragestate ();
    if (State.equals (environment.media_mounted)) {//has a stored sdcard return true;
    else {return false;

 }
  }
}
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
  android:layout_width= "Match_ Parent "
  android:layout_height=" match_parent "
  android:orientation=" vertical ">
 
  <imageview
    Android:id= "@+id/imageview"
    android:layout_width= "wrap_content"
    android:layout_height= "Wrap_content"
    android:src= "@drawable/ic_launcher"/>
 
  <button
    android:id= "@+id/buttonlocal"
    android: Layout_width= "Wrap_content"
    android:layout_height= "wrap_content"
    android:text= "local album select Avatar"/>
 
  <button
    android:id= "@+id/buttoncamera"
    android:layout_width= "wrap_content"
    android:layout_ height= "Wrap_content"
    android:text= "mobile phone photo selected Avatar"/>
 
</LinearLayout>

The above mentioned is the entire content of this article, I hope you can enjoy.

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.