当前位置: 移动技术网 > IT编程>移动开发>Android > Android通过手机拍照或从本地相册选取图片设置头像

Android通过手机拍照或从本地相册选取图片设置头像

2019年07月24日  | 移动技术网IT编程  | 我要评论

朱学虹,等你说爱我之冰山总裁,房少梅

像微信、qq、微博等社交类的app,通常都有设置头像的功能,设置头像通常有两种方式: 

1、让用户通过选择本地相册之类的图片库中已有的图像,裁剪后作为头像。 

2、让用户启动手机的相机拍照,拍完照片后裁剪,然后作为头像。 

我现在写一个简单的完整代码例子,说明如何在android中实现上述两个头像设置功能。 

mainactivity.java文件: 

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 {

 /* 头像文件 */
 private static final string image_file_name = "temp_head_image.jpg";

 /* 请求识别码 */
 private static final int code_gallery_request = 0xa0;
 private static final int code_camera_request = 0xa1;
 private static final int code_result_request = 0xa2;

 // 裁剪后图片的宽(x)和高(y),480 x 480的正方形。
 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();
  }
 });
 }

 // 从本地相册选取图片作为头像
 private void choseheadimagefromgallery() {
 intent intentfromgallery = new intent();
 // 设置文件类型
 intentfromgallery.settype("image/*");
 intentfromgallery.setaction(intent.action_get_content);
 startactivityforresult(intentfromgallery, code_gallery_request);
 }

 // 启动手机相机拍摄照片作为头像
 private void choseheadimagefromcameracapture() {
 intent intentfromcapture = new intent(mediastore.action_image_capture);

 // 判断存储卡是否可用,存储照片文件
 if (hassdcard()) {
  intentfromcapture.putextra(mediastore.extra_output, uri
   .fromfile(new file(environment
    .getexternalstoragedirectory(), image_file_name)));
 }

 startactivityforresult(intentfromcapture, code_camera_request);
 }

 @override
 protected void onactivityresult(int requestcode, int resultcode,
  intent intent) {

 // 用户没有进行有效的设置操作,返回
 if (resultcode == result_canceled) {
  toast.maketext(getapplication(), "取消", 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.getexternalstoragedirectory(),
   image_file_name);
  croprawphoto(uri.fromfile(tempfile));
  } else {
  toast.maketext(getapplication(), "没有sdcard!", toast.length_long)
   .show();
  }

  break;

 case code_result_request:
  if (intent != null) {
  setimagetoheadview(intent);
  }

  break;
 }

 super.onactivityresult(requestcode, resultcode, intent);
 }

 /**
 * 裁剪原始的图片
 */
 public void croprawphoto(uri uri) {

 intent intent = new intent("com.android.camera.action.crop");
 intent.setdataandtype(uri, "image/*");

 // 设置裁剪
 intent.putextra("crop", "true");

 // aspectx , aspecty :宽高的比例
 intent.putextra("aspectx", 1);
 intent.putextra("aspecty", 1);

 // outputx , outputy : 裁剪图片宽高
 intent.putextra("outputx", output_x);
 intent.putextra("outputy", output_y);
 intent.putextra("return-data", true);

 startactivityforresult(intent, code_result_request);
 }

 /**
 * 提取保存裁剪之后的图片数据,并设置头像部分的view
 */
 private void setimagetoheadview(intent intent) {
 bundle extras = intent.getextras();
 if (extras != null) {
  bitmap photo = extras.getparcelable("data");
  headimage.setimagebitmap(photo);
 }
 }

 /**
 * 检查设备是否存在sdcard的工具方法
 */
 public static boolean hassdcard() {
 string state = environment.getexternalstoragestate();
 if (state.equals(environment.media_mounted)) {
  // 有存储的sdcard
  return true;
 } else {
  return false;
 }
 }
}

布局文件有三个组件:放置头像的imageview,两个button,其中一个button触发从本地相册选取图片作为头像的操作时间;另外一个button触发手机拍摄照片作为头像的操作事件。

activity_main.xml: 

<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="本地相册选取头像" />

  <button
    android:id="@+id/buttoncamera"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="手机拍照选取头像" />

</linearlayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网