一、布局申明

<ImageView
android:id="@+id/head_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerHorizontal="true"
android:background="@drawable/default_avatar" />

二、Activity中代码

private ImageView headImageView;

private BitmapUtil bitmapUtil = new BitmapUtil(this);
private File headFile; headImageView = (ImageView) findViewById(R.id.head_image); headImageView.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { new android.app.AlertDialog.Builder(RegisterActivity.this)
.setTitle("头像选择")
.setNegativeButton("相册选取",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
bitmapUtil.doCropPhoto(RegisterActivity.this);
}
})
.setPositiveButton("相机拍照",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {// 判断是否有SD卡
bitmapUtil.doTakePhoto(RegisterActivity.this);// 用户点击了从照相机获取
}
}
}).show();
}
});

三、Activity回调方法

/**
* 头像选择回调
* resultCode: 正常返回-1 用户后退返回0
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("TAG", requestCode + " : " + resultCode);
if(resultCode == RESULT_OK) {
switch (requestCode) { case BitmapUtil.activity_result_camara_with_data: // 拍照
try {
if (bitmapUtil.tempFile != null) {
bitmapUtil.cropImageByCamera();
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case BitmapUtil.activity_result_cropimage_with_data:
try {
if (bitmapUtil.tempFile != null) {
headFile = bitmapUtil.tempFile; Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(headFile));
headImageView.setImageBitmap(bitmap);
}
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}

四、工具类

public class BitmapUtil {

    public final static int activity_result_camara_with_data = 1006;
public final static int activity_result_cropimage_with_data = 1007; public File tempFile; private Activity activity; public BitmapUtil(Activity activity) {
super();
this.activity = activity;
} /**
* 照相获取图片
*/
public void selectPicFromCamera() {
// if (!CommonUtils.isExitsSdcard()) {
// Toast.makeText(activity, "SD卡不存在,不能拍照", Toast.LENGTH_SHORT).show();
// return;
// } if(createNewFile()) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile)); activity.startActivityForResult(intent, activity_result_camara_with_data);
} } /**
* 照相获取完成图片时候裁剪图片
*/
public void cropImageByCamera() {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(Uri.fromFile(tempFile), "image/*");
buildCropIntent(intent); activity.startActivityForResult(intent, activity_result_cropimage_with_data);
} /**
* 从图库获取图片
*/
public void selectPicFromLocal() {
Intent intent;
if (Build.VERSION.SDK_INT < 19) {
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
} else {
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
} if(createNewFile()) {
buildCropIntent(intent); activity.startActivityForResult(intent, activity_result_cropimage_with_data);
}
} /**
* 构建截图的intent
* @param intent
*/
private void buildCropIntent(Intent intent) {
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", false); // no face detection
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
} /**
* 创建新文件
* @return
*/
private boolean createNewFile() {
if (!CommonUtils.isExitsSdcard()) {
Toast.makeText(activity, "SD卡不存在", Toast.LENGTH_SHORT).show();
return false;
}
tempFile = new File(Environment.getExternalStorageDirectory().getPath() + "/stchat" + "/images/" + System.currentTimeMillis() + ".jpg");
if(!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
} if(!tempFile.exists()) {
try {
tempFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
} public static void setBitmap(ImageView view, String head_portrait) {
// 设置用户头像
Bitmap bitmap = BitmapFactory.decodeFile(head_portrait);
if(bitmap != null) {
view.setImageBitmap(bitmap);
}
}
}

android 头像选择以及裁剪的更多相关文章

  1. android头像选择(拍照,相册,裁剪)

    组织头像上传时候,不兼容android6.0,并且 imageview.setImageBitmap(BitmapFactory.decodeFile(IMAGE_FILE_LOCATION));// ...

  2. Jcrop+uploadify+php实现上传头像预览裁剪

    最近由于项目需要,所以做了一个上传头像预览并且可以预览裁剪的功能,大概思路是上传的图片先保存到服务器,然后通过ajax从服务器获取到图片信息,再利用Jcrop插件进行裁剪,之后通过PHP获取到的四个裁 ...

  3. Android第三方开源图片裁剪截取:cropper

     Android第三方开源图片裁剪截取:cropper 很多app都需要裁剪截取图片作为头像.logo之类,而cropper是github上的一个针对Android平台的.第三方开源图片裁剪截取项 ...

  4. 选择Android还是选择JavaEE?

    很多同学咨询过同样的一个问题,该问题也是最备受争议的问题,那就是到底是选择Android还是选择JavaEE.下面发表一些本人的看法.       Android属于一个特有的Java技术应用,专注于 ...

  5. android单选按钮选择,RadioGroup,radioButton

    android单选按钮选择,RadioGroup,radioButton 14. 四 / android基础 / 没有评论   单选布局绑定 如何识别选择

  6. Android研究之为基于 x86 的 Android* 游戏选择合适的引擎具体解释

     摘要 游戏开发者知道 Android 中蕴藏着巨大的机遇. 在 Google Play 商店的前 100 款应用中,约一半是游戏应用(在利润最高的前 100 款应用中.它们所占的比例超过 90% ...

  7. MTK Android 设置-选择日期格式 [管理和组织首选项,ListPreference,CheckBoxPreference,EditTextPreference,RingtonePreference]

    ###android.preference.ListPreference的一些特性 android:key  选项的名称或键 android:title  选项的标题 android:summary  ...

  8. android选择和裁剪图像拍摄的图像

    转载请注明出处:http://blog.csdn.net/allen315410/article/details/39994913 近期从曾经的项目中扒下来一个经常使用的模块.在这里有必要记录一下的. ...

  9. Android自定义控件实例,圆形头像(图库 + 裁剪+设置),上传头像显示为圆形,附源码

    Android项目开发中经常会遇见需要实现圆角或者圆形的图片功能,如果仅仅使用系统自带的ImageView控件显然无法实现此功能,所以通过系列文章的形式由简到繁全方位的介绍一下此功能的实现,巩固一下自 ...

随机推荐

  1. javascript es6 语法快速入门

    1. let.const 作用:let 声明变量,const 声明常量 与 var 的区别:不能重复声明,且存在块级作用域,即只在代码块内生效 2. 箭头函数 使用: let show = funct ...

  2. mssql查询表在哪个数据库中

    mssql查询表在哪个数据库中 EXEC sp_MSforeachdb @command1='IF object_id(''?'' + ''..表名'') IS NOT NULL PRINT ''?' ...

  3. Neo4j中实现自定义中文全文索引

    数据库检索效率时,一般首要优化途径是从索引入手,然后根据需求再考虑更复杂的负载均衡.读写分离和分布式水平/垂直分库/表等手段:索引通过信息冗余来提高检索效率,其以空间换时间并会降低数据写入的效率:因此 ...

  4. javascript字符串方法学习汇总

    1.charAt(index) charAt(index):返回字符串中指定位置的字符 var str = 'abcdefghi'; console.log(str.charAt()); // 输出 ...

  5. Xcode常见路径

    模拟器安装的位置: /Library/Developer/CoreSimulator/Profiles/Runtimes 可以通过Xcode安装  模拟器程序的沙盒 Xcode编译生成的Product ...

  6. [转]nginx简易教程

    安装 nginx官网下载地址 发布版本分为 Linux 和 windows 版本. 也可以下载源码,编译后运行. 从源代码编译 Nginx 把源码解压缩之后,在终端里运行如下命令: $ ./confi ...

  7. leetcode-157周赛-5216-统计元音字母序列的数目

    题目描述: 方法:倒推 class Solution(object): def countVowelPermutation(self, n): MOD = 10 ** 9 + 7 a=e=i=o=u= ...

  8. 【JZOJ6345】ZYB建围墙

    description analysis 打表找规律,自认为样例给的提示很明显 容易想到最优方案是让家庭尽量先围成一个正六边形,剩下的在最外层绕一个圈 手推一波可以知道,如果正六边形有\(n\)层,剩 ...

  9. thinkphp DEFINED标签

    DEFINED标签用于判断某个常量是否有定义,用法如下: 大理石平台检验标准 <defined name="NAME"> NAME常量已经定义 </defined ...

  10. Spring+Mybatis常见问题随笔

    错误1:无法绑定指定方法 异常堆栈:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 原因 ...