Java – compresses 3 images


People who can take a look at this class need to compress those images

private list listPhotosRecovery = new ArrayList <> ();
private list listURLphotos = new ArrayList <> ();

@Override
protected void onCreate (bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_access_add);

// basic settings
storage = ConfigurationFirebase.getFirebaseStorage ();

// validate permissions
Permissions.validPermissions (permissions, this, 1);

initializeComponents ();
loadingDataSpinner ();

}

public void saveAddress () {

dialog = new SpotsDialog.Builder ()
.setContext (this)
.setMessage ("save ad")
.setCancelable (false)
.build ();
dialog.show ();

/ **
* Save image in memory
* /
for (int i = 0; i <listPhotosRecovery.size (); i ++) {
String urlImage = listPicturesRecovered.get (i);
int sizeList = listPicturesRecovered.size ();
saveFotoStorage (urlImage, SizeList, i);
}

}

private void saveFotoStorage (String urlString, final int totalPhotos, int counter) {

// create nodes in memory
StorageReference imagemAnncio = storage.child ("images")
.child ("Show")
.child (ad.getIdAddress ())
.child ("image" + counter);

// urlString = imageBit.toString (); // Test conversion

// ByteArrayOutputStream baos = new ByteArrayOutputStream ();
// imageBit.compress (Bitmap.CompressFormat.JPEG, 60, baos);
// byte[] dataImage = baos.toByteArray ();

// Upload file
UploadTask uploadTask = imageAdd.putFile (Uri.parse (urlString));

uploadTask.addOnSuccessListener (new OnSuccessListener() {
@Override
public void onSuccess (UploadTask.TaskSnapshot taskSnapshot) {

Uri firebaseUrl = taskSnapshot.getDownloadUrl ();
String urlConvertida = firebaseUrl.toString ();

listURLPhotos.add (urlConverted);

if (totalphotos == listaURLPhotos.size ()) {
advertisement.setPhotos (listHIGHLists);
advertisement.save ();
dialog.dismiss ();
finish ();
}

}
}). addOnFailureListener (new OnFailureListener () {
@Override
public void onFailure (@NonNull Exception e) {
displayErrorMessage ("Upload failed");
Log.i ("INFO", "Upload Error:" + e.getMessage ());
}
});

}

private announcement setAddress () {

String state = fieldState.getSelectedItem (). ToString ();
String Category = CategoryCategory.getSelectedItem (). ToString ();
String title = fieldTitle.getText (). ToString ();
String value = fieldValue.getText (). ToString ();
String phone = fieldTelephone.getText (). ToString ();
String description = fieldDescricao.getText (). ToString ();

Advertising advertising = new advertising ();
announcement.setState (state);
listing.setCategory (category);
ad.setTitle (title);
ad.setValue (value);
ad.setTelephone (phone);
announcement.setDescription (description);

Feedback;

}

public void validateDisplay (view view) {

advertising = setAdvertising ();
String value = String.valueOf (fieldValue.getRawValue ());

if (listPicturesRecovered.size ()! = 0) {
if (! announcement.getState (). isEmpty ()) {
if (! announcement.getCategory (). isEmpty ()) {
if (! announcement.getTitle (). isEmpty ()) {
if (! value.isEmpty () &&! value.equals ("0")) {
if (! announcement.getTelephone (). isEmpty ()) {
if (! announcement.getDescription (). isEmpty ()) {

saveAnnual ();

} else {
displayMessageError ("Fill in the description field");
}
} else {
displayMessageError ("Fill the phone field");
}
} else {
displayMessageError ("Fill value field");
}
} else {
displayMessageError ("Fill in the title field");
}
} else {
displayMessageError ("Fill in the category field");
}
} else {
displayMessageError ("Fill in the status field");
}
} else {
displayErrorMessage ("Select at least one photo!");
}

}

private void displayMessageError (String text) {
Toast.makeText (this, text, Toast.LENGTH_SHORT) .show ();
}

@Override
public void onClick (View v) {
Log.d ("onClick", "onClick:" + v.getId ());
switch (v.getId ()) {
case R.id.imageCastastro1:
Log.d ("onClick", "onClick:");
choose picture (1);
break;
case R.id.imageCadastro2:
choose picture (2);
break;
case R.id.imageCadastro3:
choose picture (3);
break;
}

}

public void pickImage (int requestCode) {
Intent i = new Intent (Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult (i, requestCode);
}



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

if (resultCode == Activity.RESULT_OK) {

// restore image
Uri imageSelected = data.getData ();

String pathImage = imageSelected.toString ();

// set image in ImageView
if (requestCode == 1) {
image1.setImageURI (selected image);
} else if (requestCode == 2) {
image2.setImageURI (selected image);
} else if (requestCode == 3) {
imagem3.setImageURI (selected image);
}

listPhotosRecovery.add (pathImage);


}

}

private void loadDadosSpinner () {

/ * String[] states = new String[]{
"SP", "MT"
}; * /

// set state spinner
string[] states = getResources (). getStringArray (R.array.states);
array adapter adapter = new ArrayAdapter(
this, android.R.layout.simple_spinner_item,
States
);
adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item);
fieldSet.setAdapter (adapter);

// Set category moth
string[] categories = getResources (). getStringArray (R.array.categories);
array adapter adapterCategory = new ArrayAdapter(
this, android.R.layout.simple_spinner_item,
Categories
);
adapterCategory.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item);
fieldCategory.setAdapter (adapterCategory);


}

private void initializeComponents () {

fieldTitle = findViewById (R.id.editTitle);
fieldDesCricao = findViewById (R.id.editDescricao);
fieldValue = findViewById (R.id.editValue);
fieldTelephone = findViewById (R.id.editTelephone);
fieldState = findViewById (R.id.spinnerState);
fieldCategory = findViewById (R.id.spinnerCategory);
image1 = findViewById (R.id.imageCadastro1);
image2 = findViewById (R.id.imageCadastro2);
image3 = findViewById (R.id.imageCadastro3);
image1.setOnClickListener (this);
image2.setOnClickListener (this);
image3.setOnClickListener (this);

// Set to pt -> English BR -> Brazil
Locale locale = new Locale ("pt", "BR");
fieldValue.setLocale (locale);

}

@Override
public void onRequestPermissionsResult (int requestCode, @NonNull String[] Permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult (requestCode, permissions, grantResults);

for (int permissaoResult: grantResults) {
if (permissaoResultado == PackageManager.PERMISSION_DENIED) {
alertValidationPermission ();
}
}

}

private void alertValidationPermission () {

Builder for AlertDialog.Builder = new AlertDialog.Builder (this);
builder.setTitle ("Denied permissions");
builder.setMessage ("Permission must be accepted to use the app");
builder.setCancelable (false);
builder.setPositiveButton ("Confirm", new DialogInterface.OnClickListener () {
@Override
public void on Click (DialogInterface dialog, int which) {
finish ();
}
});

AlertDialog dialog = builder.create ();
dialog.show ();

}

}