2
I am creating an app in which in the main Fragment a Recyclerview displays a list of registered animals. Images are being saved to Storage with the name = Animal Id + ". jpeg". I retrieve the list, images appear, but some fail. Then I go in and out the app, and they all show up. Then I go in and out, one or a few fail. And so it goes. When I finish registering, it also does not recover, but the default image appears in Imageview. But in the losers when I enter and exit the app, Imageview disappears, staying as in the image.
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Pets pet = pets.get(position);
holder.nome.setText(pet.getNome());
holder.idade.setText("Idade: " + pet.getIdade());
holder.descricao.setText("Descrição: " + pet.getDescricao());
StorageReference storage = ConfiguracaoFirebase.getFirebaseStorage();
StorageReference imageRef = storage.child("imagens").child("pets").child(pet.getIdCadastrante()).child(pet.getId()).child(pet.getId()+".jpeg");
holder.ivPet.setImageResource(R.drawable.logotipo_menor);
imageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Glide.with(context)
.load(uri)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.error(R.drawable.logotipo_menor)
.into(holder.ivPet);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Glide.with(context)
.load(R.drawable.logotipo_menor)
.fitCenter()
.into(holder.ivPet);
e.printStackTrace();
}
});
}
This is the Onbindviewholder. In the pet, I have the ID and ID information of the user who registered it. To save the image, I use this path shown in the pictureRef.
Here is the code I use to save the image, on the registration screen.
public void salvarImagem(Pets pet){
//recuperarDadosDaImagem para o firebase
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imagem.compress(Bitmap.CompressFormat.JPEG, 70, baos);
byte[] dadosImagem = baos.toByteArray();
//salvar imagem no firebase
final StorageReference imagemRef = storage.child("imagens").child("pets").child(idUser).child(pet.getId()).child(pet.getId() + ".jpeg");
UploadTask uploadTask = imagemRef.putBytes(dadosImagem);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//Toast.makeText(CadastrarPetActivity.this, "Erro ao fazer upload da imagem", Toast.LENGTH_LONG).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//Toast.makeText(CadastrarPetActivity.this, "Sucesso", Toast.LENGTH_LONG).show();
}
});
}
I’ve used a Log. D to see if the path information gets lost these times, but no. The PET and registration id, which are part of the image path, appears every time.