I have had a problem for weeks and still can not solve, did a lot of research and tested many codes, but nothing solved. I will explain my problem in detail.
I am making an application where in a layout there would be three or more clickable photos, I am using the “ArthurHub / Android-Image-Cropper” image cropping library, the steps are:
- Click on the image, which gives the option to open the image gallery to select a photo or take a new photo, after that I can crop the selected image.
- Select the second photo, do the same as the previous one, thereafter.
- Upload the photos to the server.
Steps 1 is working correctly, the problem arises when I try to select from the second image. What I'm using: I'm using two classes: the one that requests the images and the one that returns: 1. would be the main window with ImageButton. 2. and the other class that returns the selected images to the first class. (Contains the function that calls the gallery or camera, cuts the image, the other “onActivityResult” function that returns the image address, who asked for the picture). So far everything works correctly.
Problem Description: The problem arises from the second image selected. when the second class returns everything to the first, it is as if the second image is stored in the same memory space as the first (erasing everything previously), and if I select the third image, it deletes the second and only remains the third and henceforth. What I want to do is select the images and have them all seen at the same time (usable) to send to a server.
Solutions I tried:
- After days of searching, the suggestion was to make several returns on “onActivityResult” so that it returned the result of selecting images separately, I couldn't find anything that worked, the explanations I found were only halfway (including the official documentation does not detail the steps to control the various returns of the function, is very superficial), could not control the separate pointing of the images.
switch (requestCode){
case (1000):
Intent intent1 = new Intent(TirarFoto1.this, RegistrarAutomovelDuasRodas.class);
intent1.putExtra("class", classname);
intent1.putExtra("imageview", imageview);
intent1.putExtra("pathimage", pathimage);
startActivity(intent1);
break;
case (2000):
Intent intent2 = new Intent(TirarFoto1.this, RegistrarAutomovelDuasRodas.class);
intent2.putExtra("class", classname);
intent2.putExtra("imageview", imageview);
intent2.putExtra("pathimage", pathimage);
startActivity(intent2);
case (3000):
Intent intent3 = new Intent(TirarFoto1.this, RegistrarAutomovelDuasRodas.class);
intent3.putExtra("class", classname);
intent3.putExtra("imageview", imageview);
intent3.putExtra("pathimage", pathimage);
startActivity(intent3);
break;
default: break;
}
- I decided to save each image separately outside the “temporary cache” folder (where they are located), even with the “manifest” permissions, nothing happens, permission is denied, so I couldn't even create the new folder to save the images in it, as a result does not save the selected images.
if (ContextCompat.checkSelfPermission(TirarFoto1.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
File file = new File(Environment.getExternalStoragePublicDirectory(String.valueOf(imageUri))+"folderName");
if (!file.exists()){
Toast.makeText(this, "exists", Toast.LENGTH_SHORT).show();
}
if (success){
Toast.makeText(this, "creaty", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "Erro!!!", Toast.LENGTH_SHORT).show();
}
}else {
requestStoragePermisson();
}
Result: The first solution did not work and the second did not work. What I want: a model, a concrete way to help me, can anyone please help me with this? I have been stuck in this problem for a long time.
me to use ArthurHub-Android-Image-Cropper for cropped image, you can learn from this link
https://github.com/ArthurHub/Android-Image-Cropper/wiki
hope this help
Edit: i have a class using ArthurHub-Android-Image-Cropper but from kotlin and i try to convert to java like this
//for identify image
private Int imageNo;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//......
//set button image 1
final Button button1 = findViewById(R.id.button_image1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// set image count to 1
imageNo=1
getImageClick()
}
});
//set button image 2
final Button button1 = findViewById(R.id.button_image1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// set image count to 2
imageNo=2
getImageClick()
}
});
//set button image 3
final Button button1 = findViewById(R.id.button_image1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// set image count to 3
imageNo=3
getImageClick()
}
});
}
//select image form camera or galery
public void getImageClick() {
CropImage.startPickImageActivity(this);
}
//this for Crope Image
private void startCropImageActivity(Uri imageUri) {
CropImage.activity(imageUri)
.start(this);
}
@Override
@SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//handle result from onGetimageClick(button for select image from camera or galery)
if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == AppCompatActivity.RESULT_OK) {
Uri imageUri = CropImage.getPickImageResultUri(this, data);
//start Crope image
startCropImageActivity(imageUri);
}
// handle result of CropImageActivity
else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
Uri resultUri = result.getUri();
switch (imageNo){
case (1):
//here you have resultUri for save image or preview as image1
break;
case (2):
//here you have resultUri for save image or preview as image1
case (3):
//here you have resultUri for save image or preview as image1
}
}
}
don't forget to add manifest
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Theme.AppCompat"/>
Hope this help