2

I am trying to crop an image (which I've chosen from the gallery or taken from my camera) and pass it to another activity which should upload the image to my firebase storage.

At first (without the image cropping) everything went well. I was able to pass and upload the image...but after adding the image-crop-code to my onActivityResult it just closes the imageCropActivity after pressing 'crop' and jumps back to my MainActivity.

I've tried but I really cannnot figure out why it does that. Maybe I'm missing something out. Any ideas?

For the image cropping I'm using this libary: https://github.com/ArthurHub/Android-Image-Cropper

Main Activity

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        mImageUri_gallery = data.getData();
        mImageUri_camera = data.getData();


        if (requestCode == 100 && resultCode == RESULT_OK) {
            CropImage.activity(mImageUri_gallery)
                    .setGuidelines(CropImageView.Guidelines.ON)
                    .start(this);
            if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
                CropImage.ActivityResult result = CropImage.getActivityResult(data);
                if (resultCode == RESULT_OK) {
                    Uri resultUriGallery = result.getUri();
                    Intent intentGallery = new Intent(MainActivity.this, ImagePostActivity.class);
                    intentGallery.putExtra("image-uri", resultUriGallery.toString());
                    startActivity(intentGallery);
                } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                    Exception error = result.getError();
                }
            }

        } else if (resultCode != RESULT_CANCELED && data != null) {
            if (requestCode == 200 && resultCode == RESULT_OK) {
                CropImage.activity(mImageUri_camera)
                        .setGuidelines(CropImageView.Guidelines.ON)
                        .start(this);
                if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
                    CropImage.ActivityResult result = CropImage.getActivityResult(data);
                    if (resultCode == RESULT_OK) {
                        Uri resultUriCamera = result.getUri();
                        Intent intentCamera = new Intent(MainActivity.this, ImagePostActivity.class);
                        intentCamera.putExtra("image-uri", resultUriCamera.toString());
                        startActivity(intentCamera);
                    } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                        Exception error = result.getError();
                    }
                }

            }
        }


    }

ImagePostActivity

public class ImagePostActivity extends AppCompatActivity {

    public static TextView txt_btn_post;
    public static EditText mPostTitle;
    public static ImageView imagePostPreview;
    private StorageReference mStorage;
    private DatabaseReference mDatabase;
    private ProgressDialog mProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_post);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mStorage = FirebaseStorage.getInstance().getReference();
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Test_Stream_Uploads");
        mProgress = new ProgressDialog(this);
//        mPostTitle = (EditText) findViewById(R.id.mPostTitle);
        imagePostPreview = (ImageView) findViewById(R.id.imagePostPreview);
        Intent intent = getIntent();

        mImageUri_gallery = Uri.parse(intent.getStringExtra("image-uri"));
        imagePostPreview.setImageURI(mImageUri_gallery);
        mImageUri_camera = Uri.parse(intent.getStringExtra("image-uri"));
        imagePostPreview.setImageURI(mImageUri_camera);

        txt_btn_post = (TextView) findViewById(R.id.txt_btn_post);
        txt_btn_post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startPosting();
            }
        });


    }

    private void startPosting() {

        mProgress.setMessage("Bild wird hochgeladen...");
        mProgress.show();
        if (mImageUri_gallery != null) {
            StorageReference filepath = mStorage.child("Stream_Image").child(mImageUri_gallery.getLastPathSegment());
            filepath.putFile(mImageUri_gallery).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Uri downloadUrl = taskSnapshot.getDownloadUrl();
                    Intent intent = new Intent(getBaseContext(), MainActivity.class);
                    DatabaseReference newPost = mDatabase.push();
                    newPost.child("image").setValue(downloadUrl.toString());
                    mProgress.dismiss();
                    startActivity(intent);
                }
            });
        }


    }
}
Ronaldo
  • 774
  • 3
  • 8
  • 29

1 Answers1

0

I'm also actively using this tool and I actually didn't really got into your code but I can offer you an example of how it works for me. If you look into it you can easily follow and get the idea. It works for the images you take from the gallery.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
        Uri imageUri = data.getData();
        CropImage.activity(imageUri)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setAspectRatio(144,81)
                .start(this);
    }
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
            Uri resultUri = result.getUri();
            ib_image.setImageURI(resultUri);
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception error = result.getError();
        }
    }
}

If you want to apply this to the image you just took from the camera, you'll need to create your own uri, it's a bit long way but it's explained in the Android Documentation:(https://developer.android.com/training/camera/photobasics.html#TaskPhotoView), this I've explained in a discussion here: Image capture with camera & upload to Firebase (Uri in onActivityResult() is null)

Community
  • 1
  • 1
Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52