GitHub

public class MeFragment extends BaseFragment implements View.OnClickListener {private String TAG = getClass().getSimpleName();private ImageView ivPhoto;private static final int CAMERA = 1;// 拍照private static final int GALLERY = 2;// 从相册中选择private static final int PHOTO_REQUEST_CUT = 3;// 结果private final String IMAGE_FILE_LOCATION = Environment.getExternalStorageDirectory() + "/temp.jpg"; //temp fileprivate AlertDialog dialog;private View dialogView;private View backgroundView;private boolean isBackground;public static MeFragment newInstance(String param1, String param2) {MeFragment fragment = new MeFragment();Bundle args = new Bundle();return fragment;}@Overrideprotected void initView() {backgroundView=bind(R.id.ivPhotoBg);ivPhoto = bind(R.id.ivPhoto);dialogView=LayoutInflater.from(getActivity()).inflate(R.layout.dialog_select_image,null);String uri = ShareUtils.getPhotoUri();String uriBackground=ShareUtils.getPhotoBackground();if(!TextUtils.isEmpty(uri)){ivPhoto.setImageURI(Uri.parse(uri));}if(!TextUtils.isEmpty(uriBackground)){setBackground(Uri.parse(uriBackground));}dialog = new AlertDialog.Builder(getActivity(), R.style.fullScreen).create();initListener(this,R.id.ivPhoto,R.id.ivPhotoBg);initListener(this,dialogView.findViewById(R.id.btCamera),dialogView.findViewById(R.id.btPhoto),dialogView.findViewById(R.id.btCancel));}@Overridepublic int getRootLayoutId() {return R.layout.fragment_me;}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btCamera:setPhotoIntent(CAMERA);break;case R.id.btPhoto:setPhotoIntent(GALLERY);break;case R.id.ivPhoto:isBackground=false;dialog.setCancelable(true);dialog.show();dialog.setContentView(dialogView);break;case R.id.ivPhotoBg:isBackground=true;dialog.setCancelable(true);dialog.show();dialog.setContentView(dialogView);break;}}/*** 为选择的模式设置intent参数,调用系统相机 系统相册 系统裁剪* @param type GALLERY CAMERA PHOTO_REQUEST_CUT* */private void setPhotoIntent(int type) {Intent intent = new Intent();switch (type) {case PHOTO_REQUEST_CUT:break;case GALLERY:intent.setAction(Intent.ACTION_GET_CONTENT);intent.setType("image/*");startActivityForResult(intent, GALLERY);break;case CAMERA:Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(camera, CAMERA);break;}}/*** 调用系统裁剪功能:* 传入图片uri* @param uri picture uri*/private void startPhotoZoom(Uri uri) {Log.i(TAG, " url================" + uri);Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(uri, "image/*");// crop为true是设置在开启的intent中设置显示的view可以剪裁intent.putExtra("crop", "true");// aspectX aspectY 是宽高的比例intent.putExtra("aspectX", 1);intent.putExtra("aspectY", 1);// outputX,outputY 是剪裁图片的宽高intent.putExtra("outputX", 600);intent.putExtra("outputY", 600);intent.putExtra("return-data", true);intent.putExtra("noFaceDetection", true);startActivityForResult(intent, PHOTO_REQUEST_CUT);}@TargetApi(Build.VERSION_CODES.JELLY_BEAN)@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);Log.i(TAG, String.format("request code = %d result code = %d   data is null %b", requestCode, resultCode, data == null));if(dialog!=null){dialog.dismiss();}if (resultCode != Activity.RESULT_OK || data == null) {Toast.makeText(getActivity(), "you have cancel option", Toast.LENGTH_SHORT).show();return;}switch (requestCode) {case CAMERA:// 当选择拍照时调用if(isBackground){setBackground(data.getData());}else{startPhotoZoom(data.getData());//相册返回uri丢给系统裁剪}break;case GALLERY:// 当选择相册时if(isBackground){setBackground(data.getData());Log.i(TAG,data.getDataString());// uri content://com.android.providers.media.documents/document/image%3A32507ShareUtils.setPhotoBackground(data.getDataString());}else{startPhotoZoom(data.getData());//相册返回uri丢给系统裁剪}break;case PHOTO_REQUEST_CUT:// 返回的结果Bitmap bitmap = data.getParcelableExtra("data");//裁剪后,取出bitmap界面显示ShareUtils.setPhotoUri(IMAGE_FILE_LOCATION);saveBitmap(bitmap);ivPhoto.setImageBitmap(bitmap);break;}}@TargetApi(Build.VERSION_CODES.JELLY_BEAN)private void setBackground(Uri uri) {InputStream inputStream;Drawable drawable=null;try {Log.i(TAG," uri "+uri);inputStream = getActivity().getContentResolver().openInputStream(uri);drawable=Drawable.createFromStream(inputStream,"dataString");} catch (FileNotFoundException e) {e.printStackTrace();}if(drawable!=null){backgroundView.setBackground(drawable);Log.d(TAG,"set ok");}else{Log.e(TAG,"setBackground error");}}/*** 把头像保存在本地* @param bm* @return*/private Uri saveBitmap(Bitmap bm) {File tmpDir = new File(IMAGE_FILE_LOCATION);tmpDir.delete();if (!tmpDir.getParentFile().exists()) {tmpDir.mkdir();}File img = new File(IMAGE_FILE_LOCATION);try {FileOutputStream fos = new FileOutputStream(img);bm.compress(Bitmap.CompressFormat.PNG, 85, fos);fos.flush();fos.close();Toast.makeText(getActivity(), "保存成功了", Toast.LENGTH_SHORT).show();return Uri.fromFile(img);} catch (FileNotFoundException e) {Toast.makeText(getActivity(), "保存失败", Toast.LENGTH_SHORT).show();e.printStackTrace();return null;} catch (IOException e) {e.printStackTrace();Toast.makeText(getActivity(), "保存失败", Toast.LENGTH_SHORT).show();return null;}}/*** 1、bitmap  to  uri* <p/>* Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null,null));* <p/>* 2、uri  to  bitmap* <p/>* Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);* 读取图片属性:旋转的角度** @param path 图片绝对路径* @return degree旋转的角度*/public static int readPictureDegree(String path) {int degree = 0;try {ExifInterface exifInterface = new ExifInterface(path);int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);switch (orientation) {case ExifInterface.ORIENTATION_ROTATE_90:degree = 90;break;case ExifInterface.ORIENTATION_ROTATE_180:degree = 180;break;case ExifInterface.ORIENTATION_ROTATE_270:degree = 270;break;}} catch (IOException e) {e.printStackTrace();}return degree;}/*** 旋转图片** @param angle* @param bitmap* @return Bitmap*/public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {//旋转图片 动作Matrix matrix = new Matrix();matrix.postRotate(angle);System.out.println("angle2=" + angle);// 创建新的图片Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(), matrix, true);return resizedBitmap;}
}

android 调用原生相机相关推荐

  1. android 4.4原生相机,android—— cordova调用原生相机

    额..相关的下载和集成就不说了,直接说说怎么调用原生相机吧. 首先,先添加相关的插件 cordova plugin add cordova-plugin-camera 然后,瞅瞅自己插件有木有加进去 ...

  2. Android调用系统相机拍照并保存到指定位置

    Android调用系统相机拍照并保存到指定位置 @Click(R.id.btn_takePhoto)void onclick() {Intent intent = new Intent(MediaSt ...

  3. android固定位置拍照,Android调用系统相机拍照并保存到指定位置

    Android调用系统相机拍照并保存到指定位置 @Click(R.id.btn_takePhoto) void onclick() { Intent intent = new Intent(Media ...

  4. android 调用系统相机拍照 获取原图

    博客源址:android 调用系统相机拍照 获取原图 博客时间:2013-04-23 11:08 好吧,为了这个问题又折腾了一整天.之前在网上找来的方法,如果在onActivityResult中直接用 ...

  5. React Native手动实现调用原生相机相册(Android端)

    前言 最近一直在学习RN的相关开发,想做一部分调用原生的实现,来练练手,于是就有了下面这个demo,由于本人是Android开发,所以只实现了Android端的效果. Demo 主要实现 这种调用原生 ...

  6. android native 相册,React Native手动实现调用原生相机相册(Android端)

    前言 最近一直在学习RN的相关开发,想做一部分调用原生的实现,来练练手,于是就有了下面这个demo,由于本人是Android开发,所以只实现了Android端的效果. Demo 主要实现 这种调用原生 ...

  7. 5页面调用原生相机_React Native与原生通信全梳理(iOS端)

    emmm-- 先说个题外话,时隔一年,再遇RN,较之以前唯一不同的一点就是遇到的坑终于有人先踩了本文会通过原生与RN页面相互跳转.方法间的相互调用.以及H5页面调用原生页面进而调用RN页面等方面来阐述 ...

  8. Android调用系统相机和相册(更换微信头像)

    最近做了调用系统相机和相册,在其他博客中看到还有对图像进行剪切,大家都知道,我们在玩微信的时候,头像更换是方形图片,接下来我们就对这种情况具体进行描述: 必要的权限: <uses-permiss ...

  9. android调用系统相机返回图片模糊

    最近需要用到调用系统相机拍摄照片的功能,于是就直接调用系统的相机进行拍照,发现返回的bitmap存储之后都是非常模糊的 调用系统相机拍摄照片,由于StartActivityForResult过时,改用 ...

最新文章

  1. Adobe推出HTML5动画设计工具Edge
  2. oracle is ref cursor java_oracle cursor 用法总结
  3. MySQL ADDDATE(d,n) 计算起始日期 d 加上 n 天的日期
  4. [PHP] 项目实践中使用的IOC容器思想
  5. 将 C++ 中的 goto 语句转化为非 goto 语句
  6. SELECT语句“加锁选项”功能说明
  7. C++ 11 深度学习(一)auto、头文件防卫、引用、常量
  8. linux string
  9. 初中数学最全几何模型_老师熬夜整理:初中数学最全几何模型大汇总,学生大呼“过瘾”...
  10. java实现发送QQ邮件
  11. Win10 Powershell ssh到WSL
  12. 通过shell脚本防止端口扫描
  13. LinkedList的底层实现
  14. 大疆水弹机器人_现身央视的大疆机甲大师 S1 备受瞩目的教育机器人圆了儿时机甲梦...
  15. 测试总结报告写法简单总结
  16. 计算机考研吉大东北大学大连理工,考研就选985/211!这7所985高校却一个A+学科都没有,认真的吗?...
  17. 如何做一个优秀的管理者
  18. JavaScript实现简单星星闪烁特效
  19. Linux下可回收删除
  20. 展示正在活动时间内的活动,过期活动不显示

热门文章

  1. 2021 末尾冒了个泡
  2. 程序员观影有感--滚蛋吧,肿瘤君 (不喜勿喷)
  3. 三星s10刷linux,刷机神器TWRP布支持三星Galaxy S10+ 刷机不怕砖
  4. 2022 中国开源先锋 33 人评选启动,快来推荐你心尖上的开源人物吧!
  5. 使用叉叉助手写手机脚本 下载编译器+环境准备
  6. Datawhale第23期组队学习—深度学习推荐系统—task5 DIN
  7. FL Studio21中文版D编曲数字音乐工作软件
  8. 2013云计算架构师峰会之二 盛大开幕与会议感想
  9. 苹果cmsv10泡泡影视自适应美化简洁高端简约模板
  10. 大鱼号发布文章的方法