Android-image-cropper: Only allow camera, NOT galery

Created on 23 Mar 2018  路  7Comments  路  Source: ArthurHub/Android-Image-Cropper

Hi,

When starting crop image with: CropImage.activity().start(this);, the intent chooser thingie pops up and prompts the user to select camera/gdrive/galery etc...is there a way to enforce only camera here? I want to force the user to take a new picture.

Regards,

Willem

Most helpful comment

I face the same issue, but I tried to get a rid of using the traditional way to take photo from camera first
https://developer.android.com/training/camera/photobasics.html

And then access it from FileProvider in onActivityResult

if (requestCode == REQUEST_CAMERA_TAKE_PHOTO && resultCode == this.RESULT_OK) {
            File photoFile = new File(mCurrentPhotoPath);
            if (photoFile.exists()) {
                Uri photoURI = FileProvider.getUriForFile(HomePageActivity.this,
                        "com.example.android.fileprovider",
                        photoFile);
                cropImage(photoURI);
            }
        }

 private void cropImage(Uri uri) {
        int height = (int) UIUtils.dip2px(160);
        int width = (int) UIUtils.dip2px(160);
        Intent intent = CropImage.activity(uri)
                .setCropShape(CropImageView.CropShape.OVAL)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setMinCropWindowSize(width, height)
                .setMaxCropResultSize(width, height)
                .getIntent(HomePageActivity.this);
        startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
    }

For me, it works quite nature.

You may have this issue using the above method
https://github.com/ArthurHub/Android-Image-Cropper/issues/455

All 7 comments

I face the same issue, but I tried to get a rid of using the traditional way to take photo from camera first
https://developer.android.com/training/camera/photobasics.html

And then access it from FileProvider in onActivityResult

if (requestCode == REQUEST_CAMERA_TAKE_PHOTO && resultCode == this.RESULT_OK) {
            File photoFile = new File(mCurrentPhotoPath);
            if (photoFile.exists()) {
                Uri photoURI = FileProvider.getUriForFile(HomePageActivity.this,
                        "com.example.android.fileprovider",
                        photoFile);
                cropImage(photoURI);
            }
        }

 private void cropImage(Uri uri) {
        int height = (int) UIUtils.dip2px(160);
        int width = (int) UIUtils.dip2px(160);
        Intent intent = CropImage.activity(uri)
                .setCropShape(CropImageView.CropShape.OVAL)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setMinCropWindowSize(width, height)
                .setMaxCropResultSize(width, height)
                .getIntent(HomePageActivity.this);
        startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
    }

For me, it works quite nature.

You may have this issue using the above method
https://github.com/ArthurHub/Android-Image-Cropper/issues/455

@birjubhatt
It is just my own custom function to convert the dp to px since different devices have different ratio on dp.

@charlesng ok...thanks

@charlesng i have one question also....if i use your method for camera crop image what i have to do if i use open camera from recyclerview adapter ??

i currently use

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
((Activity) context).startActivityForResult(intent, CAMERA_REQUEST);

for camera open and in main activity i called adapter's onactivity result

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

if i use your suggestion to private void cropImage(Uri uri) {} you use startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);

how can i manage them if i already in my activity result ??

@birjubhatt
What is adapter.onActivityResult(requestCode, resultCode, data) ? Is it the function of RecyclerAdapter?

@charlesng sorry for my bad english ...i dont know how to explain in english verywell...

its my custom method...its call when onactivity result call from main activity.....

i done it....i just change some point from your method

i understand your code sometime letter...thanks again..

i missed to check startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);

condition in my code...

like you put
if (requestCode == REQUEST_CAMERA_TAKE_PHOTO && resultCode == this.RESULT_OK) {
}
i add ...
else if( CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.getParcelable("data");
}
that gives me result of your cropImage(Uri uri) {startActivityForResult } 's result ....

@birjubhatt
For me ,I still improve my english =[

else if( CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.getParcelable("data");
}

This part is exactly the way Android official did, but it is only the thumbnail of the image. If you want to use the full-size , use the uri and send to the CropImageActivity is better.

My suggestion is not the way this library provided, but at least a workaround for this issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zemingzeng picture zemingzeng  路  5Comments

ghost picture ghost  路  8Comments

cgupta3131 picture cgupta3131  路  7Comments

rahulwadhai picture rahulwadhai  路  5Comments

Transformat picture Transformat  路  5Comments