Android upload avatar Code, camera, album, crop

Source: Internet
Author: User

Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >


<button
Android:id= "@+id/btn_01"
Android:layout_width= "150dip"
android:layout_height= "50dip"
Android:text= "album"/>

<button
Android:id= "@+id/btn_02"
Android:layout_width= "150dip"
android:layout_height= "50dip"
android:text= "Photo"/>

<imageview
Android:id= "@+id/imageid"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"/>

</LinearLayout>

Mainactivity.java

Package com.example.uploadhead;

Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.InputStream;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;

Import Android.net.Uri;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.provider.MediaStore;
Import android.app.Activity;
Import android.content.Intent;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;

Import Android.view.View;

Import Android.widget.Button;
Import Android.widget.ImageView;

public class Mainactivity extends Activity {

public static final int NONE = 0;
public static final int photo_camera = 1;//camera photo
public static final int photo_compile = 2; Edit Picture
public static final int photo_resoult = 3;//result

ImageView Headimageview = null;
Button Photo_button = null;
Button Camera_button = null;
Private String ImageName;

public static String Getstringtoday () {
Date currenttime = new Date ();
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyymmddhhmmss");
String datestring = Formatter.format (currenttime);
return datestring;
}



@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Headimageview = (ImageView) Findviewbyid (R.id.imageid);//Find the ImageView to fill

Photo_button = (Button) Findviewbyid (r.id.btn_01);//album Button
Camera_button = (Button) Findviewbyid (r.id.btn_02);//Camera button


Open the system's album
Photo_button.setonclicklistener (New View.onclicklistener () {
public void OnClick (View v) {
Set the intention to invoke the System album (implicit Intent)
Intent Intent = new Intent ();

Set Value activity//android.intent.action.pick
Intent.setaction (Intent.action_pick);

Set type and data
Intent.setdataandtype (
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");

Open the system's album
Startactivityforresult (Intent, photo_compile);
}
});




Turn on the system camera
Camera_button.setonclicklistener (New View.onclicklistener () {

public void OnClick (View v) {
Set the name of the picture
ImageName = "/" + getstringtoday () + ". jpg";

Set the intent to invoke the system camera (implicit intent)
Intent Intent = new Intent (mediastore.action_image_capture);

Set the output path and file name of the photo

File file= new file (Environment.getexternalstoragedirectory (), ImageName);

Intent.putextra (Mediastore.extra_output, Uri.fromfile (file));
Turn on the camera
Startactivityforresult (Intent, Photo_camera);
}
});

}

Call Startactivityresult, return the callback function after
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
if (ResultCode = = NONE)
Return

Picture taking pictures by camera
if (Requestcode = = Photo_camera) {
Set file save path place it in the directory below.
File picture = new file (Environment.getexternalstoragedirectory ()
+ ImageName);
Crop a picture
Startphotozoom (Uri.fromfile (picture));
}

if (data = = NULL)
Return

Reading album cropping pictures
if (Requestcode = = Photo_compile) {
Crop a picture
Startphotozoom (Data.getdata ());
}


Crop photo processing results
if (Requestcode = = Photo_resoult) {

Bundle extras = Data.getextras ();

if (extras! = null) {

Bitmap photo = extras.getparcelable ("Data");

Bytearrayoutputstream stream = new Bytearrayoutputstream ();
Photo.compress (Bitmap.CompressFormat.JPEG, (), stream);//(0-
100) Compress files

Set up picture display content
Headimageview.setimagebitmap (photo);
}

}

Super.onactivityresult (Requestcode, ResultCode, data);
}


/** to call the cropping picture function of the system
*
* @param the path of the URI picture
*/
public void Startphotozoom (Uri uri) {

Intent Intent = new Intent ("Com.android.camera.action.CROP");
Intent.setdataandtype (URI, "image/*");
Intent.putextra ("Crop", "true");
Aspectx Aspecty is the ratio of width to height
Intent.putextra ("Aspectx", 1);
Intent.putextra ("Aspecty", 1);
Outputx Outputy is the cropped picture width high
Intent.putextra ("Outputx", 64);
Intent.putextra ("Outputy", 64);
Intent.putextra ("Return-data", true);

Startactivityforresult (Intent, Photo_resoult);
}

}

Android upload avatar Code, camera, album, crop

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.