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

像微信、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>

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

时间: 2024-09-08 11:30:10

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

Android调用手机拍照功能的方法_Android

本文实例讲述了Android调用手机拍照功能的方法.分享给大家供大家参考.具体如下: 一.main.xml布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" andr

Android调用手机拍照功能的方法

本文实例讲述了Android调用手机拍照功能的方法.分享给大家供大家参考.具体如下: 一.main.xml布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" andr

解决android有的手机拍照后上传图片被旋转的问题_Android

需求:做仿新浪发微博的项目,能够上传图片还有两外一个项目用到手机拍摄图片,这两个都需要把图片上传到服务器 遇到问题:有的手机拍摄的图片旋转90度,有的图片旋转了180度,有的手机是正常的,服务器要求的是正的,这样问题就来了,不能用户发个照片在微博上看到的是被旋转了的啊,另外一个项目里旋转了的图片直接匹配出现问题,这个更严重. 解决:开始的时候在网上没有找到很好的解决办法,谷歌百度的搜了一通,想到第一种解决方式,当手机拍照结束,在返回结果处理里面立即跳转到一个新的页面,在新的页面里让用户自己手动去

解决android有的手机拍照后上传图片被旋转的问题

需求:做仿新浪发微博的项目,能够上传图片还有两外一个项目用到手机拍摄图片,这两个都需要把图片上传到服务器 遇到问题:有的手机拍摄的图片旋转90度,有的图片旋转了180度,有的手机是正常的,服务器要求的是正的,这样问题就来了,不能用户发个照片在微博上看到的是被旋转了的啊,另外一个项目里旋转了的图片直接匹配出现问题,这个更严重. 解决:开始的时候在网上没有找到很好的解决办法,谷歌百度的搜了一通,想到第一种解决方式,当手机拍照结束,在返回结果处理里面立即跳转到一个新的页面,在新的页面里让用户自己手动去

街拍良器 Android特色手机拍照软件复古相机

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 夏天已经姗然而至.街头的帅哥靓女开始穿的凉爽,喜欢街拍的朋友可千万不要错过了这个好时节.对于很多Android手机用户来说,Android的自带摄像软件向来都不是很给力,功能非常的中规中矩,毫无特色可言.今天,泡椒小编凌少我就给大家推荐一款功能给力,充满特色的Android手机拍照软件--复古相机. 测试手机:MOTO 里程碑1代 手机平台:

Android实现手机拍照功能_Android

本文实例为大家讲解如何轻松实现Android手机拍照功能,分享给大家供大家参考.具体如下: 一.布局文件main.xml <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"

Android部分手机拍照后获取的图片被旋转问题的解决方法

调用Android系统拍照功能后,三星手机拍摄后的照片被旋转了90度,横着拍给你变成竖的,竖的拍给你变成横的.其它品牌的手机都是正常的,就三星出现这个怪事. 在Android适配上,我原来一直以为国内的小米手机够奇葩了,结果还有更奇葩的!你说你没事旋转照片干啥,实在是猜不透其居心何在,纯粹是在给开发者制造麻烦啊! 解决办法是获取到拍照后照片被旋转的角度,再旋转回去就好了. 具体思路: 1.首先在调用拍照方法时,保存拍照后的相片原图,得到原图路径,(PhotoBitmapUtils是我自己写的一个

android调用系统拍照程序和从图库选取图片,返回后调用系统裁剪工具

调用系统拍照 Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File myImageDir = new File(TEMP_TAKE_PHOTO_FILE_PATH); //创建图片保存目录if (!myImageDir.exists()) {Mylog.d(THIS, "Create the path:" + TEMP_TAKE_PHOTO_FILE_PATH);myImageDir.mkdirs();} //根据时间

Android实现从本地图库/相机拍照后裁剪图片并设置头像_Android

先给大家展示效果图: 代码部分: 布局代码(其实就是两个按钮和一个ImageView来显示头像) <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="v