For my college CBT I'm developing an android app. I stopped at a point where I'm downloading the URL of some Firebase Storage images, and within the onSuccess(Uri uri)
method, I have access to the uri
variable where the URL of the image returns to me so I can switch to Picasso or Glider and download. But the thing is that I can not get this uri value to handle outside of the onSucess()
method.
Below is my code:
ListView Locallista;
List<DataProvider> tracks;
DataProvider track;
String uid;
String iid;
Uri bitmap;
View header;
String key;
AdaptadorCuston adaptador;
private FirebaseAuth auth;
Query databaseTracks;
FirebaseDatabase databaseTrack;
FirebaseStorage storage;
StorageReference storageReference;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.tela_principal);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("HunterFood Fornecedor");
setSupportActionBar(toolbar);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
uid = user.getUid();
}
auth = FirebaseAuth.getInstance();
databaseTrack = FirebaseDatabase.getInstance();
storage = FirebaseStorage.getInstance();
databaseTracks =
databaseTrack.getReference("produtos").orderByChild("id").equalTo(uid);
storageReference = storage.getReferenceFromUrl("gs://hunterfood-
a7f41.appspot.com/pictures_prod/");
tracks = new ArrayList<>();
Locallista = (ListView) findViewById(R.id.lista);
header = (View) getLayoutInflater().inflate(R.layout.celula_lista,null);
Locallista.addHeaderView(header);
carregarDados();
Locallista.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int
position, long id) {
Object o = Locallista.getItemAtPosition(position);
Intent t = new Intent(getApplicationContext(),
tela_editar.class);
startActivity(t);
}
});
}
protected void onStart() {
super.onStart();
}
protected void onResume(){
super.onResume();
}
private void carregarDados() {
databaseTracks.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
tracks.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
track = postSnapshot.getValue(DataProvider.class);
iid = postSnapshot.getKey();
track.setKey(iid);
storageReference.child(iid +
".jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>
(){
@Override
public void onSuccess(Uri uri) {
//enter code here
bitmap = uri;
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "Download Completou", Toast.LENGTH_SHORT);
toast.show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "Download
Falhou", Toast.LENGTH_SHORT);
toast.show();
}
});
track.setIcone(bitmap);
tracks.add(track);
}
adaptador = new
AdaptadorCuston(tela_principal.this,R.layout.celula_lista, tracks);
Locallista.setAdapter(adaptador);
}
@Override
public void onCancelled(DatabaseError databaseError) {
String mensagem = databaseError.getMessage();
Toast.makeText(getApplicationContext(), mensagem,
Toast.LENGTH_SHORT).show();
}
});
Below is the data from the AdapterCuston class:
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull
ViewGroup parent) {
View row;
row = convertView;
DataHandler handler;
if(convertView == null){
LayoutInflater inflater =
(LayoutInflater)this.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.celula_lista,parent, false);
handler = new DataHandler();
handler.imagemIcone = (ImageView)row.findViewById(R.id.img_lista);
handler.titulo = (TextView) row.findViewById(R.id.txt_titulo);
handler.descricao = (TextView) row.findViewById(R.id.txt_descricao);
row.setTag(handler);
}else{
handler = (DataHandler)row.getTag();
}
DataProvider dataProvider;
dataProvider = (DataProvider) this.getItem(position);
Glide
.with(getContext())
.load(dataProvider.getIcone())
.into(handler.imagemIcone);
handler.titulo.setText(dataProvider.getTitulo());
handler.descricao.setText(dataProvider.getDescricao());
return row;
}
private class DataHandler {
ImageView imagemIcone;
TextView titulo;
TextView descricao;
}
}