我正在使用Android Image Cropper Android Image Cropper。 我已经有以下代码,可以从图库或照相机https://github.com/ArthurHub/Android-Image-Cropper/wiki/Pick-image-for-cropping-from-Camera-or-Gallery中选择图像。
这是我当前的代码:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private Button changeImageButton;
private Uri mCropImageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
changeImageButton = (Button)findViewById(R.id.changeImage);
changeImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onSelectImageClick(v);
}
});
}
@Override
@SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// handle result of pick image chooser
if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri imageUri = CropImage.getPickImageResultUri(this, data);
// For API >= 23 we need to check specifically that we have permissions to read external storage.
if (CropImage.isReadExternalStoragePermissionsRequired(this, imageUri)) {
// request permissions and handle the result in onRequestPermissionsResult()
mCropImageUri = imageUri;
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE);
} else {
// no permissions required or already granted, can start crop image activity
startCropImageActivity(imageUri);
}
}
}
public void onSelectImageClick(View view) {
CropImage.startPickImageActivity(this);
}
private void startCropImageActivity(Uri imageUri) {
CropImage.activity(imageUri).setCropShape(CropImageView.CropShape.RECTANGLE).setFixAspectRatio(true)
.start(this);
}
}
这是activity_main xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/changeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Change Image"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
效果很好,当我单击按钮时,出现一个对话框,询问我要从哪里拍摄图像:图库,照相机,文件等。选择任何一个时,我都会进入裁剪活动。但是,单击“裁剪”后,什么都没有发生。如何在现有代码中获取裁剪后的图像,并将其设置为方形图像视图,假设尺寸为150x150dp。我不知道应该使用什么回调来保存裁剪/旋转的图像?由于该收割者似乎将几种API版本混在一起,因此我对如何执行此操作感到困惑。帮助将不胜感激
答案 0 :(得分:0)
private ImageView open_gallary;
在您的OnCreate()
内
open_gallary.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int permissionCheck = ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.CAMERA);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
permission();
}else {
CropImage.activity()
.start(getContext(), Register.this);
}
}
});
您的许可方法是:
public void permission() {
final Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Translucent_NoTitleBar);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_permission);
dialog.show();
Button cancel_log, open;
ImageView close_dialog;
open = (Button) dialog.findViewById(R.id.open);
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
final Intent i = new Intent();
i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:" + getActivity().getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
getActivity().startActivity(i);
}
});
cancel_log = (Button) dialog.findViewById(R.id.cancel_log);
cancel_log.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
}
});
close_dialog = (ImageView) dialog.findViewById(R.id.close_dialog);
close_dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
}
})
}
并使用以下命令设置图像:
Uri uri;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
uri = result.getUri();
open_gallary.setImageURI(uri);////set the image after taking it
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
别忘了
<uses-permission android:name="android.permission.CAMERA" />