ArthurHub / ArthurHub/Android-Image-Cropper
using the Image crooper with DialogFragment
- Dominant language
- Java
- Stars
- 6.4k
- Forks
- 1.4k
- PR merge metrics
- No merged PRs in 30d
Description
Hi.
I am trying to use the CropImageView object in a DialogFragment. My fragment is built in a way that I can use it as a fragment or as a DialogFragment. when I use it as a fragment it works ok. but when I am trying to use it as a DialogFragment the CropImageView won't show the selected image.
is the CropImageView can work with dialogFragment?
how can I implement it in a DialogFragment?
Thanks
Ben
**this is the fragment:**
```
public class MainFragment extends DialogFragment implements
CropImageView.OnGetCroppedImageCompleteListener, CropImageView.OnSetImageUriCompleteListener, View.OnClickListener {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_IMAGE_URI = "ImageUri";
private static final String ARG_IS_DIALOG = "IsDialog";
private CropImageView mCropImageView;
private Uri mCropImageUri;
private View mProgressView;
private TextView mProgressViewText;
private Button mCropImageBtn;
private OnFragmentInteractionListener mListener;
private AlertDialog.Builder mDialogBuilder;
private ViewGroup mContainer;
private boolean mIsDialog;
public MainFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param imageUri Image Uri.
* @return A new instance of fragment MainFragment.
*/
// TODO: Rename and change types and number of parameters
public static MainFragment newInstance(Uri imageUri, boolean isDialog) {
MainFragment fragment = new MainFragment();
Bundle args = new Bundle();
args.putString(ARG_IMAGE_URI, imageUri.toString());
args.putBoolean(ARG_IS_DIALOG, isDialog);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mCropImageUri = Uri.parse(getArguments().getString(ARG_IMAGE_URI));
mIsDialog = getArguments().getBoolean(ARG_IS_DIALOG);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (mIsDialog){
if(!getShowsDialog()) // AVOID REQUEST FEATURE CRASH
{
mContainer = container;
return super.onCreateView(inflater, container, savedInstanceState);
}
else
{
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_main, container, false);
intCropImageView(view);
return view;
}
}else {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_main, container, false);
intCropImageView(view);
return view;
}
}
private void intCropImageView(View view) {
mCropImageView = (CropImageView) view.findViewById(R.id.CropImageView);
mProgressView = view.findViewById(R.id.ProgressView);
mProgressViewText = (TextView) view.findViewById(R.id.ProgressViewText);
mCropImageBtn = (Button) view.findViewById(R.id.btn_crop_image_fragment);
mCropImageBtn.setOnClickListener(this);
setInitialCropRect();
mCropImageView.setOnSetImageUriCompleteListener(this);
mCropImageView.setOnGetCroppedImageCompleteListener(this);
}
/**
* Set the initial rectangle to use.
*/
public void setInitialCropRect() {
mCropImageView.setCropRect(new Rect(100, 300, 500, 1200));
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mCropImageView.setImageUriAsync(mCropImageUri);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
mDialogBuilder = new AlertDialog.Builder(getActivity());
View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_main, mContainer);
mDialogBuilder.setView(view);
mDialogBuilder.setTitle("crop image");
return mDialogBuilder.create();
}
/*// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}*/
@Override
public void onStart() {
super.onStart();
int dialogWidth;
int dialogHeight;
// safety check
if (getDialog() != null) {
dialogWidth = 1100; // specify a value here
dialogHeight = 1200; // specify a value here
getDialog().getWindow().setLayout(dialogWidth, dialogHeight);
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
mCropImageView.setOnSetImageUriCompleteListener(null);
mCropImageView.setOnGetCroppedImageCompleteListener(null);
}
@Override
public void onGetCroppedImageComplete(CropImageView view, Bitmap bitmap, Exception error) {
mProgressView.setVisibility(View.INVISIBLE);
if (error == null) {
if (bitmap != null) {
mCropImageView.setImageBitmap(bitmap);
}
} else {
Log.e("Crop", "Failed to crop image", error);
Toast.makeText(getContext(), "Something went wrong, try again", Toast.LENGTH_LONG).show();
}
}
@Override
public void onSetImageUriComplete(CropImageView view, Uri uri, Exception error) {
mProgressView.setVisibility(View.INVISIBLE);
if (error != null) {
Log.e("Crop", "Failed to load image for cropping", error);
Toast.makeText(getContext(), "Something went wrong, try again", Toast.LENGTH_LONG).show();
}
}
/**
* Crop the image and set it back to the cropping view.
*/
public void onCropTheImageClick(View view) {
mCropImageView.getCroppedImageAsync();
mProgressViewText.setText("Cropping...");
mProgressView.setVisibility(View.VISIBLE);
}
@Override
public void onClick(View v) {
onCropTheImageClick(v);
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.