CropActivity is not starting in onActivityResult inside Fragment - android

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();
}
}
}

Related

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;
}

onActivityResult user cancels the activity

I have a activity call for getting image
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
On my onActivityResult I want to handle the scenario if the user cancels the operation in between.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
if (selectedImagePath != null) {
//do something
}
else
Log.i("At the","selectedimagenull");
}
else
Log.i("At the","SELECT PICTURENULL");
}
}
But if the user cancels the operation nowhere am getting information. I've tried to debug but its not catching anywhere. What am doing wrong here ?
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
if (selectedImagePath != null) {
//do something
}
else
Log.i("At the","selectedimagenull");
}
else
Log.i("At the","SELECT PICTURENULL");
}
else
Log.i("At the","result not ok"); // User cancelled the activity
}

Image-Crop-Activity doesn't send Image-Uri to receiving Activity (Android-Image-Cropper)

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);
}
});
}
}
}
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)

Two onActivityResults in one activity

Do you know how to handle two onActivityResult()s in one activity?
I need to use my camera and search for my photos in one activity.
public class MainActivity extends AppCompatActivity {
public static final int REQUEST_CAPTURE = 1;
Button button_Vyber_Fotku, button_Fotak;
ImageView imageView_VyberFotku, imageView_Fotak;
private static final int PICK_IMAGE = 100;
Uri imageUri_vybrana;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView_VyberFotku = (ImageView) findViewById(R.id.imageView_VyberFotku);
button_Vyber_Fotku = (Button) findViewById(R.id.button_Vyber_Fotku);
imageView_Fotak = (ImageView) findViewById(R.id.imageView_Fotak);
button_Fotak = (Button) findViewById(R.id.button_fotak);
if (!hasCamera())
{
button_Fotak.setEnabled(false);
}
}
public void Vyber_fotku_clicked(View v)
{
openGallery();
}
private void openGallery()
{
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
imageUri_vybrana = data.getData();
imageView_VyberFotku.setImageURI(imageUri_vybrana);
}
}
public boolean hasCamera()
{
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
public void PouzijFotakClicked(View v)
{
Intent vyfot = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(vyfot , REQUEST_CAPTURE);
}
#Override
protected void onActivityResult(int requestCode1, int resultCode1, Intent data1)
{ if (requestCode1 == REQUEST_CAPTURE && resultCode1 == RESULT_OK)
{
Bundle extras = data1.getExtras();
Bitmap photo = (Bitmap) extras.get("data1");
imageView_Fotak.setImageBitmap(photo);
}
}
}
Instead of two different method for onActivityResults use single method and distinguish them according to their request code.
#Override
protected void onActivityResult(int requestCode1, int resultCode1, Intent data1){
if (requestCode1 == REQUEST_CAPTURE && resultCode1 == RESULT_OK){
Bundle extras = data1.getExtras();
Bitmap photo = (Bitmap) extras.get("data1");
imageView_Fotak.setImageBitmap(photo);
}
else if (resultCode1 == RESULT_OK && requestCode1 == PICK_IMAGE){
imageUri_vybrana = data1.getData();
imageView_VyberFotku.setImageURI(imageUri_vybrana);
}
}
Note: You can't have two declaration for single override method.
// define two variable camera and pick_image of int type pass value of request code of desired out put in activity onResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
//first one to pick image
//do somthing
}else if(resultCode == RESULT_OK && requestCode == Camera){
//use to take image from camera response
}
}
Check this
It works for me
private static final int CAMERA_ = 999;
private static final int GALLERY_ = 888;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_) {
if (resultCode == RESULT_OK) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
UtilsClass.mBitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
imageView.setImageBitmap(UtilsClass.mBitmap);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}else if (requestCode == GALLERY_) {
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
UtilsClass.mBitmap = (BitmapFactory.decodeFile(picturePath));
imageView.setImageBitmap(UtilsClass.mBitmap);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}

QR Code not showing result using Zxing

I am currently working with qrcode
I added Zxing library and also core.jar.
When I am trying to scan a document not getting any results.
qrscan_button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
Toast.makeText(this,
"Scan Result = " + data.getStringExtra("SCAN_RESULT"),
Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) {
}
}
}
I added CaptureActivity class in Manifest.
I am using Zxing 2.1

Categories

Resources