Android手机拍照或选取图库图片作为头像
程序员文章站
2023-11-08 14:57:58
package zhangpgil.photo;
import java.io.file;
import android.support.v7.app...
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的正方形。(生成bitmap貌似有时要报错?可试下把大小弄小点) 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; } } }
<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>
以上所述就是本文的全部内容了,希望大家能够喜欢。
上一篇: android教程之textview解析带图片的html示例
下一篇: 羊水过少怎么办,孕妇知道吗