Android使用系统自带的相机实现一键拍照功能
程序员文章站
2024-02-20 10:50:16
今天分享的是用系统自带的相机实现一键拍照功能。
public class mainactivity extends appcompatactivity {
pr...
今天分享的是用系统自带的相机实现一键拍照功能。
public class mainactivity extends appcompatactivity { private static final int take_photo = 100; private imageview iv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); iv = (imageview) findviewbyid(r.id.iv); } public void btn(view view) { switch (view.getid()){ case r.id.button: //跳转到拍照的意图 intent takephotointent = new intent(mediastore.action_image_capture); //判断系统中是否有照相机 if (takephotointent.resolveactivity(getpackagemanager())!=null){ startactivityforresult(takephotointent,take_photo); } break; } } /** * 处理数据 * @param requestcode * @param resultcode * @param data */ @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); //判断请求码和返回码 if (requestcode==take_photo&&resultcode==result_ok){ bitmap bitmap = data.getparcelableextra("data"); iv.setimagebitmap(bitmap); } } }