头像的截取 包括相册和相机
程序员文章站
2022-06-01 12:11:48
...
或不多说 上代码
权限呢需要内存的读取权限
先写隐式启动相册相机,这里使用的是startActivityForResult
/*相册*/
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 500);
/*相机*/
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
startActivityForResult(intent, 1000);
接下来就是一个Activity的回调
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 500 && resultCode == RESULT_OK) {
Uri uri = data.getData();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
intent.putExtra("return-data", true);
startActivityForResult(intent, 200);
}
if (requestCode == 1000 && resultCode == RESULT_OK) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(Uri.fromFile(new File(path)), "image/*");
intent.putExtra("crop", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
intent.putExtra("return-data", true);
startActivityForResult(intent, 200);
}
if (requestCode == 200 && resultCode == RESULT_OK) {
Bitmap bitmap = data.getParcelableExtra("data");
img_tou.setImageBitmap(bitmap);
}
}