您的位置:首页 > 移动开发 > Android开发

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);
});

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: