0

In my app the user presses a button that uploads their image to Firebase storage and simultaneously brings them to the page with the result. After they have uploaded the image I need to run some commands locally which then uploads the result to Firebase storage. I have the result displaying properly when the image is already in Firebase storage (code used below) but I can't get it to work if I add the image after the user presses the button, is there a way of doing this?

ImageView image = findViewById(R.id.rImage);

    Picasso.Builder builder = new Picasso.Builder(this);
    builder.listener(new Picasso.Listener()
    {
        @Override
        public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception)
        {
            exception.printStackTrace();
        }
    });
    builder.build().load("https://url").into(image);
tech123
  • 13
  • 1
  • 7
  • Could you pls explain a little bit more what you want to do and maybe add more of your code. The code shown is not doing any routing to a result. – Tarik Huber Apr 28 '21 at 13:20
  • @TarikHuber I took out the url link for security reasons, it is a url to the result image in Firebase storage. So at the moment if the result is already in storage and the user presses upload the result will display but in practice when the user presses upload the result won't be in Firebase storage yet so I want to add listeners that will display the image once it is uploaded to Firebase storage – tech123 Apr 28 '21 at 13:24

1 Answers1

0

I hope I got the question right. I would upload the image with a code like this:

final StorageReference ref = storageRef.child("images/your_file_name.png");
uploadTask = ref.putFile(file);
ImageView image = findViewById(R.id.rImage);

Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
    @Override
    public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
        if (!task.isSuccessful()) {
            throw task.getException();
        }

        // Continue with the task to get the download URL
        return ref.getDownloadUrl();
    }
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
    @Override
    public void onComplete(@NonNull Task<Uri> task) {
        if (task.isSuccessful()) {
            Uri downloadUri = task.getResult();
            
             Picasso.Builder builder = new Picasso.Builder(this);
    builder.listener(new Picasso.Listener()
    {
        @Override
        public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception)
        {
            exception.printStackTrace();
        }
    });
    builder.build().load(downloadUri).into(image);

        } else {
            // Handle failures
            // ...
        }
    }
});
Tarik Huber
  • 7,061
  • 2
  • 12
  • 18
  • I have tried using storage reference previously spending days on it without success see my previous post [link] (https://stackoverflow.com/questions/67255293/downloading-image-from-firebase-storage-into-imageview-on-android-studio-java) for reference and the code that I posted is the only code that seems to work to display the image. I tried the code you provided but it keeps crashing. Is there a way of adding listeners to my existing code? If I used an onSuccess and onFailure listener would I just need to add my last line of code into the onSuccess? – tech123 Apr 28 '21 at 14:34