Android 拍照,选取照片,截取,显示

一.拍照与选取照片

使用方法例子

/**
@
*/
 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 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);
                        });


            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Android拍照或者从相册选取图片加载在imageview上的具体步骤: 1. 在AndroidManifest.xml文件中添加相机和存储权限: ``` <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` 2. 在布局文件中添加一个ImageView: ``` <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent"/> ``` 3. 在Activity中定义以下变量: ``` private static final int REQUEST_IMAGE_CAPTURE = 1; private static final int REQUEST_IMAGE_PICK = 2; private ImageView imageView; private Uri imageUri; ``` 4. 在需要拍照或者从相册选取图片的地方,添加以下代码: 拍照: ``` Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { File photoFile = null; try { photoFile = createImageFile(); } catch (IOException e) { e.printStackTrace(); } if (photoFile != null) { imageUri = FileProvider.getUriForFile(this, "com.example.fileprovider", photoFile); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); } } ``` 从相册选取图片: ``` Intent pickPhotoIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(pickPhotoIntent, REQUEST_IMAGE_PICK); ``` 5. 在Activity中添加以下方法: ``` @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { if (requestCode == REQUEST_IMAGE_CAPTURE) { imageView.setImageURI(imageUri); } else if (requestCode == REQUEST_IMAGE_PICK) { imageUri = data.getData(); imageView.setImageURI(imageUri); } } } private File createImageFile() throws IOException { String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File imageFile = File.createTempFile(imageFileName, ".jpg", storageDir); return imageFile; } ``` 以上就是Android拍照或者从相册选取图片加载在ImageView上的完整代码,希望可以帮助到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值