How to open only camera and crop using theartofdev in android - android

how to open only camera and crop using theartofdev dependency .I am using theartofdev dependency 2.7.+ .

Try this
//Declare in Manifest file
<activity
android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="#style/Base.Theme.AppCompat" />
// Declare global variable
Uri mCropImageUri;
// write below line on the button click
CropImage.startPickImageActivity(this);
// then result will received inside onActivityResult() method
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
getImageCallback(requestCode,resultCode,data);
}
public void getImageCallback(int requestCode, int resultCode, Intent data) {
if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri imageUri = CropImage.getPickImageResultUri(this, data);
// For API >= 23 we need to check specifically that we have permissions to read external storage.
if (CropImage.isReadExternalStoragePermissionsRequired(this, imageUri)) {
// request permissions and handle the result in onRequestPermissionsResult()
mCropImageUri = imageUri;
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE);
} else {
// no permissions required or already grunted, can start crop image activity
startCropImageActivity(imageUri);
}
}
else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
profileImage.setImageURI(resultUri);
}
}
}
private void startCropImageActivity(Uri imageUri) {
CropImage.activity(imageUri).setGuidelines(CropImageView.Guidelines.ON).setCropShape(CropImageView.CropShape.OVAL).start(this);
}

Related

No callback for startActivityForResult in case of MediaStore.INTENT_ACTION_VIDEO_CAMERA

The case is when user selects camera, he has the flexibility to either capture image or record video and the user shall be able to show the same in one's app.
For this case, MediaStore.INTENT_ACTION_VIDEO_CAMERA intent is used but no callback comes in either case of capturing image or recording video.
Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
startActivityForResult(intent, VIDEO_CAMERA);
For getting result, code used as follows:-
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
return;
}
if (resultCode == RESULT_OK) {
if (requestCode == VIDEO_CAMERA) {
Uri uri = data.getData();
}
}
}
To start the camera and get the result back in onActivityResult(), you should create an Intent with MediaStore.ACTION_IMAGE_CAPTURE:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Similarly, to capture a video, use MediaStore.ACTION_VIDEO_CAPTURE
I finally found a solution and it is as follows:-
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Intent chooserIntent = Intent.createChooser(takePictureIntent, "Capture Image or Video");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takeVideoIntent});
startActivityForResult(chooserIntent,VIDEO_CAMERA);
And finally receives the callback in onActivityResult and I got the uri in this way:-
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
return;
}
if (resultCode == RESULT_OK) {
if (requestCode == VIDEO_CAMERA) {
Uri uri;
if (data == null || data.getData() == null) {
Bitmap bitmap (Bitmap)data.getExtras().get("data");
// TODO:Get uri from bitmap for image
uri = getImageUri(context, bitmap);
} else {
//Get uri for video
uri = data.getData();
}
}
}
}

Android pick images from gallery not working

I am trying to choose an image from device's gallery, crop it and then display it in an imageView. After I click on an image in image selector, the app quits with logcat message showing V/FA: Inactivity, disconnecting from the service. I am running a service in background to get data from firestore.
I have also tried using cropping libraries like https://github.com/ArthurHub/Android-Image-Cropper but the behaviour was same.
I also tried the following.
FABfile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
});
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null){
mImageUri = data.getData();
imageView.setImageURI(result.getUri());
}
}
Please help to resolve this issue.
Use these methods to choose image from gallery:
public void showGallery() {
if (hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
loadGallery();
} else {
requestPermissionsSafely(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
}
private void loadGallery() {
Intent choose = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(choose, PICK_IMAGE_GALLERY);
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
loadGallery();
}
break;
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_GALLERY) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
}
}
}
And:
#TargetApi(Build.VERSION_CODES.M)
public void requestPermissionsSafely(String[] permissions, int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(permissions, requestCode);
}
}
#TargetApi(Build.VERSION_CODES.M)
public boolean hasPermission(String permission) {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
}

CropActivity is not starting in onActivityResult inside Fragment

I am calling an intent to select an image and later to crop the image into aspect ratio(1,1), but when i run the app the gallery is opening but when i select the image it closes and gets back to the Fragment.
Below is the code of my Button's on click listener
mImageBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(galleryIntent,
"SELECT IMAGE"), GALLERY_PICK);
}
});
this is the code for onActivityResult
public void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_PICK && resultCode == RESULT_OK){
Uri imageUri = data.getData();
CropImage.activity(imageUri)
.setAspectRatio(1, 1)
.setMinCropWindowSize(500, 500)
.start(getActivity());
Toast.makeText(SettingsActivity.this, imageUri,
Toast.LENGTH_LONG).show();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
{
Exception error = result.getError();
}
}
}
Crop activity is not starting and the app goes back to the fragment screen when i select the pic
Below is the code of MainActivity where i have used bottom navigation view and used OnActivity result as well
private void showPlacePicker() {
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
} catch (Exception e) {
Log.e(TAG, e.getStackTrace().toString());
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent
data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
----code---
}
}
I have used this library in one of my projects and in that I didn't explicitly create a image picker but instead let the library handle it for me. To do this,
in your code
mImageBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CropImage.activity()
.setAspectRatio(1, 1)
.setMinCropWindowSize(500, 500)
.start(getContext());
}
});
Handle the result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri imageUri = result.getUri();
//handle the image
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
//handle error
} else if(resultCode == RESULT_CANCELED){
//handle cancel case
}
}
}
For more information
In case of using appcompact fragment we cannot use .start(getActivity()) to start the crop activity.
Instead of this use:
.start(getContext(), this)
Here is code look like in fragments:
CropImage.activity(uri).setGuidelines(CropImageView.Guidelines.ON).start(getContext(), this);
ActivityResult is more suitable :
#Override
public void onActivityResult(ActivityResult activityResult) {
if (activityResult.getResultCode() == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(activityResult.getData());
}
if (activityResult.getResultCode() == RESULT_OK) {
Uri resultUri = result.getUri();
} else if (activityResult.getResultCode() == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}

How do I make downloaded images fit to Image Button?

I am using the Android-Image-Cropper library. It seems to work fine with pictures taken with the camera itself, but when I select images from the gallery that were downloaded from the internet and crop them, the image does not completely cover the ImageButton.
Here is a picture of it not working (downloaded)
Here is one that is working (taken with camera)
Here is my code from the activity:
public void setProfilePicture(View view) {
CropImage.startPickImageActivity(this);
}
#Override
#SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// handle result of pick image chooser
if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri imageUri = CropImage.getPickImageResultUri(this, data);
// For API >= 23 we need to check specifically that we have permissions to read external storage.
if (CropImage.isReadExternalStoragePermissionsRequired(this, imageUri)) {
// request permissions and handle the result in onRequestPermissionsResult()
mCropImageUri = imageUri;
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE);
} else {
// no permissions required or already granted, can start crop image activity
startCropImageActivity(imageUri);
}
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
profilepicture.setImageURI(resultUri);
} else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception e = result.getError();
}
}
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (requestCode == CropImage.PICK_IMAGE_PERMISSIONS_REQUEST_CODE) {
if (mCropImageUri != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// required permissions granted, start crop image activity
startCropImageActivity(mCropImageUri);
} else {
Toast.makeText(this, "Cancelling, required permissions are not granted", Toast.LENGTH_LONG).show();
}
}
}
private void startCropImageActivity(Uri imageUri) {
CropImage.activity(imageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
}
}
Oh wait it figured it out.
I had to set android:scaleType="fitCenter" in my XML and
.setAspectRatio(1, 1)
.setFixAspectRatio(true)
to my startCropImageActivity method.

onActivityResult returns null Intent

I am trying to capture image from camera. But it returns null Intent onActivityResult .
Here is my code
CaptureImageFromCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 7);
}
});
And onActivityResult is
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 7 && resultCode == RESULT_OK) {
try {
if(data.getData() == null) {
bitmap = (Bitmap)data.getExtras().get("data");
} else {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
}
ImageViewHolder.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I am new to Android so please explain answer in detail.
Thanks in advance..!
Start Your work by passing intent through this :
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 100);
so after capturing the image from camera use to get data about images through intent :
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri yourCapturedImage = intent.getData();
}
Make sure you have added camera permissions, write permissions in manifest and also cross verify run time permissions.
Use your Intent like below, and specify the path where captured image will be write -
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri outputUri = Uri.fromFile("path of your directory");
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
startActivityForResult(intent, 7);
You can also use setImageURI() in onActivityResult() instead of setImageBitmap()
First of all add the camera permissions in android manifest. If your android version is greater than lollipop then you need to add run time permissions See documentations here
<uses-permission android:name="android.permission.CAMERA"/>
and then your code will be
CaptureImageFromCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 7);
}
});
and then use setImageUrI() in your onActivityResult()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case 7:
/*case 7 for image results and display image in imageview via Uri*/
if (resultCode == RESULT_OK)
{
Uri imageUri = data.getData();
ImageViewHolder.setImageURI(imageUri);
}
return;
default:
return;
}
}
Uri imageUri = data.getData() will no more work..
first get bitmap from data like this:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
val imageBitmap = data.extras.get("data") as Bitmap
imageView.setImageBitmap(imageBitmap)
}
}
then save bitmap image.. please find details here:
https://developer.android.com/training/camera/photobasics

Categories

Resources