我们需要导包:app:implementation 'com.jph.takephoto:takephoto_library:4.0.3'
布局
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> -<LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/ll_button"> <Button android:layout_height="wrap_content" android:layout_width="0dp" android:id="@+id/btn_take_photo2" android:text="拍照" android:layout_weight="1" android:layout_marginLeft="30dp"/> <Button android:layout_height="wrap_content" android:layout_width="0dp" android:id="@+id/btn_choose_photo2" android:text="相册" android:layout_weight="1" android:layout_marginLeft="30dp"/> </LinearLayout> <ImageView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/iv_image" android:background="@mipmap/logo_bg" android:layout_centerHorizontal="true"/> </LinearLayout>
activity的实现
public class PhotoActivity extends TakePhotoActivity { private Button btn_take_photo2, btn_choose_photo2; ImageView imageView = null; TakePhoto takePhoto; File file; Uri imageUri; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_photo); btn_take_photo2 = (Button) findViewById(R.id.btn_take_photo2); btn_choose_photo2 = (Button) findViewById(R.id.btn_choose_photo2); imageView = (ImageView) findViewById(R.id.iv_image); initView(); //从相册中选图片 btn_choose_photo2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CropOptions cropOptions = new CropOptions.Builder().setAspectX(1).setAspectY(1).setWithOwnCrop(true).create(); takePhoto.onPickFromDocumentsWithCrop(imageUri, cropOptions); } }); //拍照 btn_take_photo2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CropOptions cropOptions = new CropOptions.Builder().setAspectX(1).setAspectY(1).setWithOwnCrop(true).create(); takePhoto.onPickFromCaptureWithCrop(imageUri,cropOptions); } }); } public void initView() { takePhoto = getTakePhoto(); CropOptions.Builder builder = new CropOptions.Builder(); builder.setAspectX(800).setAspectY(800); builder.setWithOwnCrop(true); file = new File(Environment.getExternalStorageDirectory(), "/temp/" + System.currentTimeMillis() + ".jpg"); if (!file.getParentFile().exists()) { boolean mkdirs = file.getParentFile().mkdirs(); if (!mkdirs) { Toast.makeText(this, "文件目录创建失败", Toast.LENGTH_SHORT).show(); } } imageUri = Uri.fromFile(file); CompressConfig config = new CompressConfig.Builder() .setMaxSize(102400) .setMaxPixel(400) .enableReserveRaw(true) .create(); takePhoto.onEnableCompress(config, true); } @Override public void takeSuccess(TResult result) { super.takeSuccess(result); Log.e("111",result.getImage().getCompressPath()); //这是压缩后的路径 Glide.with(this).load(result.getImage().getCompressPath()).into(imageView); //这是未压缩的路径 // Glide.with(this).load(result.getImage().getOriginalPath()).into(imageView); } @Override public void takeCancel() { super.takeCancel(); } @Override public void takeFail(TResult result, String msg) { super.takeFail(result, msg); } }源码地址下载:https://download.csdn.net/download/weixin_42267745/10432680