Stradtdog
Stradtdog

Reputation: 1550

Image crop requires double selection

I am currently using this Image crop:

api 'com.theartofdev.edmodo:android-image-cropper:2.8.+' 

I have noticed that when selecting an image. I first chose an image in my gallery. As soon as I select the image, the process restarts, giving me the screen: ava

From here I am able to go through the whole process of selecting and cropping the image. My program for running this api and selecting the image is as follows:

public class Registration3 extends AppCompatActivity {

    Button elFin;
    CircleImageView ProfileImage;
    private DatabaseReference UsersRef;
    private FirebaseAuth mAuth;
    private StorageReference UserProfileImageRef;
    String currentUserID;
    final static int Gallery_Pick = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration3);

        mAuth = FirebaseAuth.getInstance();
       currentUserID = mAuth.getCurrentUser().getUid();
        elFin = findViewById(R.id.finalform);
        ProfileImage = findViewById(R.id.circle_image);
       UsersRef = FirebaseDatabase.getInstance().getReference().child("DriversInformation").child(currentUserID);
       UserProfileImageRef = FirebaseStorage.getInstance().getReference().child("profileImages");
        ProfileImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryIntent = new Intent();
                galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
                galleryIntent.setType("image/*");
                startActivityForResult(galleryIntent, Gallery_Pick);
            }
        });

        elFin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Registration3.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == Gallery_Pick && resultCode == RESULT_OK && data != null) {
            Uri imageUri = data.getData();

            CropImage.activity()
                    .setGuidelines(CropImageView.Guidelines.ON)
                    .setAspectRatio(1,1)
                    .start(this);


        }


        if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
        {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);

            if (resultCode == RESULT_OK) {

                Uri resultUri = result.getUri();

                StorageReference filePath = UserProfileImageRef.child(currentUserID + ".jpg");

                filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                        if(task.isSuccessful()) {

                            Toast.makeText(Registration3.this, "Image Uplaoded", Toast.LENGTH_SHORT).show();

                            Task<Uri> result = task.getResult().getMetadata().getReference().getDownloadUrl();

                            result.addOnSuccessListener(new OnSuccessListener<Uri>() {
                                @Override
                                public void onSuccess(Uri uri) {
                                    final String downloadUrl = uri.toString();

                                    UsersRef.child("profileimage").setValue(downloadUrl)
                                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                                @Override
                                                public void onComplete(@NonNull Task<Void> task) {
                                                    if (task.isSuccessful()) {
                                                        Intent selfIntent = new Intent(Registration3.this, Registration3.class);
                                                        startActivity(selfIntent);

                                                        Toast.makeText(Registration3.this, "Image Uploaded", Toast.LENGTH_SHORT).show();

                                                    } else {
                                                        String message = task.getException().getMessage();
                                                        Toast.makeText(Registration3.this, "Error: " + message, Toast.LENGTH_SHORT).show();

                                                    }
                                                }
                                            });
                                }
                            });
                        }
                    }
                });
            }
            else {
                Toast.makeText(Registration3.this, "Error: ", Toast.LENGTH_SHORT).show();

            }
        }
    }
}

Does anyone know how I could prevent the dialog of selecting an image from running twice? Thank you.

Upvotes: 0

Views: 48

Answers (1)

nyko
nyko

Reputation: 36

We had the same problem and solved it using this tutorial. We can't explain where this behaviour came from, but maybe our resulting code can help you with your issue:

class CreateGroupActivity : AppCompatActivity() {

    private val GALLERY_REQUEST_CODE = 1234

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_create_group)

        // setClickListener to ImageView of XML
        crt_group_iv_avatar.setOnClickListener {
            pickFromGallery()
        }
    }

    private fun pickFromGallery(){
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
        intent.type = "image/*"
        val mimeTypes = arrayOf("image/jpeg", "image/png", "image/jpg")
        intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
        intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
        startActivityForResult(intent, GALLERY_REQUEST_CODE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode){
            GALLERY_REQUEST_CODE -> {
                if(resultCode == Activity.RESULT_OK){
                    data?.data?.let { uri ->
                        launchImageCrop(uri)
                    }
                }
            }
            CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE -> {
                val result = CropImage.getActivityResult(data)
                if(resultCode == Activity.RESULT_OK){
                    result.uri?.let {
                        // DO SOMETHING WITH THAT CROPPED IMAGE LIKE Glide.with(this).load(uri).into(imageView)
                    }
                }
            }
            CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE -> {
                Log.d("DEBUG", "Error ${CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE} occured.")
            }
        }
    }

    private fun launchImageCrop(uri: Uri) {
        CropImage.activity(uri).setGuidelines(CropImageView.Guidelines.ON)
            .setAspectRatio(1920, 1080)
            .start(this)
    }
}

Upvotes: 1

Related Questions