Android调用系统, 任意比例裁剪图片
16lz
2021-01-24
废话不多说,直接上代码
核心代码:
/** * 跳转到系统裁剪图片页面 * @param imagePath 需要裁剪的图片路径 */private void cropPic(String imagePath) { File file = new File(imagePath); Intent intent = new Intent("com.android.camera.action.CROP"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile(this, "com.leon.crop.fileprovider", file); intent.setDataAndType(contentUri, "image/*"); } else { intent.setDataAndType(Uri.fromFile(file), "image/*"); } intent.putExtra("crop", "true"); intent.putExtra("aspectX", 0.1); intent.putExtra("aspectY", 0.1); intent.putExtra("outputX", 150); intent.putExtra("outputY", 150); intent.putExtra("return-data", true); intent.putExtra("scale", true); startActivityForResult(intent, CROP_PHOTO); }
进入到裁剪页面, 可以自由选择裁剪的比例:
代码如下:
public class MainActivity extends AppCompatActivity { public final int CROP_PHOTO = 10; public final int ACTION_TAKE_PHOTO = 20; private ImageView ivImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //显示图片的imageview ivImage = (ImageView) findViewById(R.id.ivImage); //点击跳转拍照 findViewById(R.id.btnPhoto).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { takePhoto(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { File mPhotoFile = new File(getAppFile(this, "images/user_take.jpg")); switch (requestCode) { case CROP_PHOTO: //裁剪照片后 if (data != null) { Bitmap bitmap = data.getExtras().getParcelable("data"); ivImage.setImageBitmap(bitmap); } //裁剪后删除拍照的照片 if (mPhotoFile.exists()) { //noinspection ResultOfMethodCallIgnored mPhotoFile.delete(); } break; case ACTION_TAKE_PHOTO: if (mPhotoFile.exists()) { cropPic(getAppFile(this, "images/user_take.jpg")); } break; } } /** * 拍照 */ private void takePhoto() { try { Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); File file = new File(getAppFile(this, "images")); File mPhotoFile = new File(getAppFile(this, "images/user_take.jpg")); if (!file.exists()) { file.mkdirs(); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile(this, "com.pandaq.pandaeye.fileprovider", mPhotoFile); intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri); } else { intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPhotoFile)); } startActivityForResult(intent, ACTION_TAKE_PHOTO); } catch (Exception e) { e.printStackTrace(); } } /** * 获取本应用在系统的存储目录 */ public static String getAppFile(Context context, String uniqueName) { String cachePath; if ((Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) && context.getExternalCacheDir() != null) { cachePath = context.getExternalCacheDir().getParent(); } else { cachePath = context.getCacheDir().getParent(); } return cachePath + File.separator + uniqueName; } /** * 跳转到系统裁剪图片页面 * @param imagePath 需要裁剪的图片路径 */ private void cropPic(String imagePath) { File file = new File(imagePath); Intent intent = new Intent("com.android.camera.action.CROP"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile(this, "com.leon.crop.fileprovider", file); intent.setDataAndType(contentUri, "image/*"); } else { intent.setDataAndType(Uri.fromFile(file), "image/*"); } intent.putExtra("crop", "true"); intent.putExtra("aspectX", 0.1); intent.putExtra("aspectY", 0.1); intent.putExtra("outputX", 150); intent.putExtra("outputY", 150); intent.putExtra("return-data", true); intent.putExtra("scale", true); startActivityForResult(intent, CROP_PHOTO); }}
更多相关文章
- Android实现用代码简单安装和卸载APK的方法
- Android(安卓)控件之Gallery图片集
- Android实现圆角矩形和圆形ImageView的方式
- AndroidStudio中的gradle脚本文件解读
- android启动界面
- Android中对同一个TextView设置不同字体样式
- Android学习之路七:DatePicker和TimePicker
- Android(安卓)开发绕不过的坑:你的 Bitmap 究竟占多大内存?
- Android(安卓)APP启动过程中应用代码的加载