I can’t get value from the onSuccess (Uri Uri) method return

Asked

Viewed 164 times

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;
    }
}

1 answer

0


You will need to declare an instance of your DataProvider within the is and pass that instance to the method onSuccess(...), where you can assign the Uri to the correct instance of each DataProvider.

tracks.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
    final DataProvider 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) {

            track.setIcone(uri);

            ...
        }

    });
    ...
}

UPDATING

Builder of AdaptadorCuston:

public AdaptadorCuston(/*Seus parâmetros*/..., StorageReference storageReference) {
    this.storageReference = storageReference;
}

Method getView of AdaptadorCuston:

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    ...
    handler.imagemIcone = (ImageView)row.findViewById(R.id.img_lista);
    ...

    DataProvider dataProvider = (DataProvider) this.getItem(position);

    storageReference.child(dataProvider.getKey() + ".jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>(){
        @Override
        public void onSuccess(Uri uri) {

            Glide.with(getContext())
                .load(uri)
                .into(handler.imagemIcone);

            dataProvider.setIcone(uri);

            ...
        }
    });
}
  • Good afternoon, I did the informed but the same thing happens. I can’t get the value of the Uri within the onSucess method().

  • You could add the code referring to your AdaptadorCuston?

  • Good evening, I put the Adaptercuston code right in the question

  • You might consider passing the instance of storageReference into your AdaptadorCuston. And within getView(...), get the image Uri by calling storageReference.child(iid + ".jpg").getDownloadUrl().addOnSuccessListener(...). And within the onSuccess(...), use Glide to get the image

  • Thank you very much Danilo, it worked out making these modifications.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.