当前位置: 移动技术网 > 移动技术>移动开发>Android > Android实现从本地图库/相机拍照后裁剪图片并设置头像

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

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

玩qq或者是微信的盆友都知道,这些聊天工具里都要设置头像,一般情况下大家的解决办法是从本地图库选择图片或是从相机拍照,然后根据自己的喜爱截取图片。上述过程已经实现好了,最后一步我加上了把截取好的图片在保存到本地的操作,来保存头像。为了大家需要,下面移动技术网小编把完整的代码贴出来供大家参考。

先给大家展示效果图:

代码部分:

布局代码(其实就是两个按钮和一个imageview来显示头像)

<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"
/>
<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> 

正文代码:

public class mainactivity extends appcompatactivity {
/* 头像文件 */
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 = 600;
private static int output_y = 600;
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);
//如果你想在activity中得到新打开activity关闭后返回的数据,
//你需要使用系统提供的startactivityforresult(intent intent,int requestcode)方法打开新的activity
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);
<br>       //新建文件夹 先选好路径 再调用mkdir函数 现在是根目录下面的ask文件夹
file nf = new file(environment.getexternalstoragedirectory()+"/ask");
nf.mkdir();
<br>       //在根目录下面的ask文件夹下 创建okkk.jpg文件
file f = new file(environment.getexternalstoragedirectory()+"/ask", "okkk.jpg");
fileoutputstream out = null;
try {<br><br>          //打开输出流 将图片数据填入文件中
out = new fileoutputstream(f);
photo.compress(bitmap.compressformat.png, 90, out);
try {
out.flush();
out.close();
} catch (ioexception e) {
e.printstacktrace();
}
} catch (filenotfoundexception e) {
e.printstacktrace();
}
}
}
/**
* 检查设备是否存在sdcard的工具方法
*/
public static boolean hassdcard() {
string state = environment.getexternalstoragestate();
if (state.equals(environment.media_mounted)) {
// 有存储的sdcard
return true;
} else {
return false;
}
}
}

因为涉及到文件读写,要加入两个权限!!!

<uses-permission android:name="android.permission.write_external_storage"/>
<uses-permission android:name="android.permission.mount_unmount_filesystems"/> 

关于本文给大家介绍的android实现从本地图库/相机拍照后裁剪图片并设置头像的相关知识就给大家介绍到这里,希望对大家有所帮助!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网