Android - image crop not loading image

2.3k Views Asked by At

I want to cut the photo I took from the gallery or the camera. But I can't see the photo in cropping activity. It just doesn't come. I take a photo from the camera and start the cutting activity. The screen just looks like this:

enter image description here

Camera and onActivityResult

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                Log.d("FotografHata",""+ex);
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.maksu.aquarium.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        Uri uri = Uri.parse(currentPhotoPath);
        openCropActivity(uri, uri);
    } else if (requestCode == UCrop.REQUEST_CROP && resultCode == RESULT_OK) {
        Uri uri = UCrop.getOutput(data);
    }
}

openCropActivity

private void openCropActivity(Uri girenUri, Uri cikanUri) {
    UCrop.of(girenUri, cikanUri)
            .withAspectRatio(16, 9)
            .start(this);
}
1

There are 1 best solutions below

0
On

I was passing source and destination Uri by using Uri.parse(myFile) so it kept loading

What worked for me is that I used toUri(), like this

val sourceUri = myFile.toUri()