0
For my college TCC I am developing an App for android. I stopped at a point where I am downloading the URL of some Firebase Storage images, and within the method onSuccess(Uri uri)
, i have access to variable uri
where I return the image URL and then I pass to Picasso or Glider and download. But I can’t get that value from the Uri to manipulate out of the method onSucess()
.
Below follows 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 of 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;
}
}
Good afternoon, I did the informed but the same thing happens. I can’t get the value of the Uri within the onSucess method().
– Deivid Martinez da Silva
You could add the code referring to your
AdaptadorCuston
?– Danilo de Oliveira
Good evening, I put the Adaptercuston code right in the question
– Deivid Martinez da Silva
You might consider passing the instance of
storageReference
into yourAdaptadorCuston
. And withingetView(...)
, get the image Uri by callingstorageReference.child(iid + ".jpg").getDownloadUrl().addOnSuccessListener(...)
. And within theonSuccess(...)
, use Glide to get the image– Danilo de Oliveira
Thank you very much Danilo, it worked out making these modifications.
– Deivid Martinez da Silva