skip to Main Content

I can get link with this, i tested with realtime database on firebase but i’cant use link as a String.but when i write like Glide.with(this).load("link").into(imageView);

public void run() {
    imgRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            imgUrl = uri.toString();
            myImg.setValue(imgUrl);

        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            System.out.println("foto linki yollanamadı.");
        }
    });
}

thread.start();
Glide.with(this).load(imgUrl).into(imageView);

I tried lots of things. How can I solve this. I want to use as a string like that:

Glide.with(this).load(imgUrl).into(imageView);

2

Answers


  1. This line

        Glide.with(this).load(imgUrl).into(imageView);
    

    Is being called outside the thread where you are initializing imgUrl at which point the getDownloadUrl() task is not done.

    You need to call it after the task is done and from the main thread.
    Your run block should therefore look like below

        imgRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                imgUrl = uri.toString();
                myImg.setValue(imgUrl);
    //Change start
    runOnUIThread(new Runnable() {
        public void run() {
           Glide.with(this).load(imgUrl).into(imageView);                
        }
    });
    //Change end
    
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                System.out.println("foto linki yollanamadı.");
            }
        });
    }
    
    Login or Signup to reply.
  2. When you get the download URL of a file from the Firebase Storage, you actually perform an asynchronous operation. What you need to understand is that such an operation takes time. How much time it will take? We don’t know, as it depends on too many factors. So when you attach a listener to such an operation, the onSuccess() method will fire when the operation for getting the download URL is successful. This means that by the time you’re using:

    Glide.with(this).load(imgUrl).into(imageView);
    

    The operation for getting the download URL isn’t complete, that’s why the imgUrl is null. The solution is always the same. Any code that needs data from an asynchronous operation, needs to be inside the onSuccess() method or be called from there. So in your case that would be:

    public void run() {
        imgRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                imgUrl = uri.toString();
                myImg.setValue(imgUrl);
                Glide.with(this).load(imgUrl).into(imageView); //👈
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                System.out.println("foto linki yollanamadı.");
            }
        });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search