Android相机、图册demo
程序员文章站
2024-02-28 10:18:16
本文为大家分享了android相机、图册基本demo,供大家参考,具体内容如下
package com.example.democamera;
impor...
本文为大家分享了android相机、图册基本demo,供大家参考,具体内容如下
package com.example.democamera; import java.io.file; import java.io.filenotfoundexception; import java.io.ioexception; import android.net.uri; import android.os.bundle; import android.os.environment; import android.provider.mediastore; import android.app.activity; import android.content.context; import android.content.intent; import android.content.res.configuration; import android.database.cursor; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.view.view; import android.widget.imageview; public class mainactivity extends activity { private imageview iv; static final int gallery = 1, camera = 2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); iv = (imageview) findviewbyid(r.id.iv_picture); } /** * 启动图片画廊 * * @param view */ public void startgallery(view view) { intent intent = new intent(); intent.setaction(intent.action_get_content); intent.settype("image/*"); startactivityforresult(intent, gallery); } /** * 启动相机 * * @param view */ public void startcamera(view view) { intent intent = new intent(mediastore.action_image_capture); if (issdexsit()) { /* 创建存放图片文件夹 */ file dir = new file(environment.getexternalstoragedirectory() + "/my"); if (!dir.exists()) dir.mkdirs(); /* 设置图片参数 得到原尺寸的图片 */ file file = new file(dir, "aaa.jpg"); intent.putextra(mediastore.extra_screen_orientation, configuration.orientation_undefined); intent.putextra(mediastore.extra_output, uri.fromfile(file)); } startactivityforresult(intent, camera); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { // todo auto-generated method stub super.onactivityresult(requestcode, resultcode, data); switch (requestcode) { case gallery: /* 做判断,防止返回报错 */ if (data != null) { /* * 三种方式处理uri */ uri uri = data.getdata(); iv.setimageuri(uri); bitmap bitmap = bitmapfactory .decodefile(getpathbyuri(uri, this)); iv.setimagebitmap(bitmap); bitmap bitmap2 = getbitmapbyuri(uri, this); iv.setimagebitmap(bitmap2); } break; case camera: if (data != null) {// 这是获得缩略图的方法 bitmap b = (bitmap) data.getextras().get("data"); iv.setimagebitmap(b); } else {// 有sd卡得到图片原图 bitmap bitmap = bitmapfactory.decodefile("sdcard/my/aaa.jpg");//直接这样做会有发生oom的风险,demo简单这么处理 iv.setimagebitmap(bitmap); } break; default: break; } } /** * uri-->bitmap * * @param uri * @param context * @return */ public static bitmap getbitmapbyuri(uri uri, context context) { bitmap bitmap = null; try { bitmap = mediastore.images.media.getbitmap( context.getcontentresolver(), uri); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return bitmap; } /** * uri--->path * * @param uri * @param context * @return */ public static string getpathbyuri(uri uri, context context) { string path = null; string[] projection = { mediastore.images.media.data }; cursor cursor = context.getcontentresolver().query(uri, projection, null, null, null); if (cursor.movetofirst()) { int index = cursor .getcolumnindexorthrow(mediastore.images.media.data); path = cursor.getstring(index); } return path; } /** * 判断sd卡是否存在 * * @return */ public static boolean issdexsit() { if (environment.getexternalstoragestate().equals( environment.media_mounted)) { return true; } else { return false; } } }
下面分享具体android 调用相机、打开相册、裁剪图片的实现代码,内容如下
private imageview iv_user_photo; private string filename = ""; private file tempfile; private int crop = 300;// 裁剪大小 private static final int open_camera_code = 10; private static final int open_gallery_code = 11; private static final int crop_photo_code = 12; private onclicklistener popupwindowitemonclick = new onclicklistener() { @override public void onclick(view v) { menuwindow.dismiss(); switch (v.getid()) { // 拍照 case r.id.btn_camera: initfile(); opencamera(); break; // 相册 case r.id.btn_gallery: initfile(); opengallery(); break; default: break; } } }; public void initfile() { if(filename.equals("")) { if(fileutil.existsdcard()) { string path = environment.getexternalstoragedirectory() + file.separator + "janubookingonline" + file.separator; fileutil.mkdir(path); logger.i("path:" + path); filename = path + "user_head_photo.jpg"; tempfile = new file(filename); } else { commonuitl.toast(context, "请插入sd卡"); } } } /** * 调用相机 */ public void opencamera() { intent intent = new intent(mediastore.action_image_capture);// 打开相机 intent.putextra("output", uri.fromfile(tempfile)); startactivityforresult(intent, open_camera_code); } /** * 打开相册 */ public void opengallery() { intent intent = new intent(intent.action_pick);// 打开相册 intent.setdataandtype(mediastore.images.media.internal_content_uri, "image/*"); intent.putextra("output", uri.fromfile(tempfile)); startactivityforresult(intent, open_gallery_code); } /** * 裁剪图片 * @param uri */ public void cropphoto(uri uri) { intent intent = new intent("com.android.camera.action.crop"); intent.setdataandtype(uri, "image/*"); intent.putextra("output", uri.fromfile(tempfile)); intent.putextra("crop", true); intent.putextra("aspectx", 1); intent.putextra("aspecty", 1); intent.putextra("outputx", crop); intent.putextra("outputy", crop); startactivityforresult(intent, crop_photo_code); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (resultcode == 1) return; switch (requestcode) { case open_camera_code: cropphoto(uri.fromfile(tempfile)); break; case open_gallery_code: cropphoto(data.getdata()); break; case crop_photo_code: try { bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 2; bitmap bitmap = bitmapfactory.decodefile(filename, options); if (bitmap != null) { iv_user_photo.setimagebitmap(bitmap); commonuitl.sharedpreferences(context, appconstants.user_photo, filename); } } catch (exception e) { e.printstacktrace(); } break; default: break; } super.onactivityresult(requestcode, resultcode, data); }
以上就是关于android相机、图册的基本操作内容,希望对大家学习android软件编程有所帮助。