Android 拍照,选取照片,截取,显示
2016-11-24 09:27
316 查看
一.拍照与选取照片
使用方法例子
/** @ */ ImagePickerUtils.showImagePicker(this, viewModel.getAvatarPath(), 1, new ArrayList<PhotoInfo>());
工具类
public class ImagePickerUtils { public final static int REQUEST_CODE_PICKER_CAMERA = 10012; public final static int REQUEST_CODE_PICKER_ABULM = 10013; public static void showImagePicker(Activity activity, String imagepath,int maxCount, List<PhotoInfo> selectPhotoList){ List<String> ways = new ArrayList<String>(); ways.add("拍照"); ways.add("从相册选取"); AlertPromptManager.getInstance().showItemsDialog(activity, ways, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: { takePictureFromCamera(activity,imagepath); } break; case 1: { takePictureFromAlbum(activity,maxCount,selectPhotoList); } break; } } }); } public static void takePictureFromCamera(Activity activity,String imagePath){ File filename = new File(imagePath); Uri imageUri = Uri.fromFile(filename); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); activity.startActivityForResult(intent, REQUEST_CODE_PICKER_CAMERA); } /** * 点击了从本地相片中 * @param activity * @param maxCount * @param selectPhotoList */ public static void takePictureFromAlbum(Activity activity,int 4000 maxCount,List<PhotoInfo> selectPhotoList){ Intent imageIntent = new Intent(activity, SelectPhotoActivity.class); PhotoSerializable photoSerializable = new PhotoSerializable(); photoSerializable.setList(selectPhotoList); Bundle bundle = new Bundle(); bundle.putSerializable("photoSerializable",photoSerializable); imageIntent.putExtra("max", maxCount); imageIntent.putExtras(bundle); activity.startActivityForResult(imageIntent, REQUEST_CODE_PICKER_ABULM); } }
相关的类
/** * Created by Administrator on 2016/3/31 0031. */ public class PhotoSerializable implements Serializable { private static final long serialVersionUID = 1L; private List<PhotoInfo> list; public List<PhotoInfo> getList() { return list; } public void setList(List<PhotoInfo> list) { this.list = list; } }
二.结果的处理:截取成
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { Uri avatarUri = Uri.fromFile(new File(viewModel.getAvatarPath())); DisplayMetrics display = this.getResources().getDisplayMetrics(); int width = display.widthPixels; int height = display.heightPixels; if (requestCode == ImagePickerUtils.REQUEST_CODE_PICKER_CAMERA) { //拍照 //裁剪图片 Crop.of(avatarUri, avatarUri) .asSquare() .withMaxSize(width, height) .start(this); } else if (requestCode == ImagePickerUtils.REQUEST_CODE_PICKER_ABULM) { //从相册中选取 if (data == null) return; Bundle bundle = data.getExtras(); if (bundle != null) { PhotoSerializable photoSerializable = (PhotoSerializable) bundle.getSerializable("photoSerializable"); List<PhotoInfo> selectPhotos = photoSerializable.getList(); PhotoInfo photoInfo = selectPhotos.get(0); Crop.of(Uri.fromFile(new File(photoInfo.getPath_absolute())), avatarUri) .asSquare() .withMaxSize(width, height) .start(this); } } else if (requestCode == Crop.REQUEST_CROP) { viewModel.uploadAvatar() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .unsubscribeOn(Schedulers.io()) .doOnSubscribe(() -> { showLoading("上传头像中..."); }) .subscribe(success -> { RxBus.getDefault().post(new RxBusEvent(RxBusEvent.EVENT.APP_UPDATE_CHILDREN, childModel)); }, error -> { showErrorAlert(error.getLocalizedMessage()); }, () -> { showSuccessAlert("上传成功"); ivHeadPortrait.setImageURI(avatarUri); }); } } }
相关文章推荐
- Android 拍照后显示照片
- Android实现照片选择显示-从相册选择或拍照选择
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时为什么会回到home界面或者到前一个界面,然后才会跳转到当前activity
- Android完美满足你所有的拍照或从相册中选取照片
- android 调用摄像头进行拍照,并对照片进行裁剪,然后显示。
- Android获取图片资源之——拍照后在程序中显示照片
- Androd选取相册照片和拍照处理-android学习之旅(62)
- android调用系统相机拍照保存照片并显示在当前界面
- Android调用相册或相机拍照选取照片并裁剪
- android调用系统相机拍照与打开相册选择照片进行截取保存笔记
- Android调用相册或相机拍照选取照片并裁剪
- Androd选取相册照片和拍照处理-android学习之旅(62)
- Android 拍照上传和拍照本地显示,拍照剪切上传,选取本地图片剪切上传
- Android拍照 图库照片本地显示
- Android拍照后显示照片
- android--调用系统相机拍照,显示并保存照片
- android拍照并给照片添加备注,以GridView显示
- Android实现拍照或从相册中选择照片并截取
- [置顶] Android实现仿微信朋友圈发布动态(拍照、图库选择、照片压缩、显示、保存、缩略图、点击缩略图删除对应文件等)附源码
- Android调用相册或相机拍照选取照片并裁剪